# MIT 18.S191 | 计算机思维导论-Julia, 笔记中1.7节利用迭代器生成路径代码几个疑惑

``````       struct Paths
m::Int
n::Int
end

Base.iterate(p::Paths) = fill(1,p.m), fill(1,p.m) #start the iteration with 1's
Base.IteratorSize(::Type{Paths}) = SizeUnknown()

function Base.iterate(p::Paths, state)
if state ≠ fill(p.n,p.m)  # end when each row has an n
newstate = next(state,p.n)
return newstate, newstate
end
end

function next(path,n)
k = length(path)
# start from the end and find the first element that can be updated by adding 1
while  k≥2 && ( path[k]==n || path[k]+1 > path[k-1]+1 )
k -= 1
end
path[k] +=1 #add the one then reset the following elements
for j = k+1 : length(path)
path[j] = max(path[j-1]-1,1)
end
return(path)
end

function allpaths(m,n)
v=Vector{Int}[]
paths = Paths(m,n)
for p ∈ paths
push!(v,copy(p))
end
v
end
``````

``````let
paths1=Paths(6,6)
v=Vector{Int}[]
for p in paths1
push!(v, copy(p))
end
length(v), v
end
``````

1。对于问题1的回应。我刚开始也是这么想，但是对于下面这段代码。

``````aaa=[[1,1,1,1], [2,2,2,2], [3, 3, 3, 3]]
v1 = []
v2 = []
for a in aaa
push!(v1, a)
push!(v2, copy(a))
end
v1 == v2
``````

2。对于问题2，因为在不等式两边同时加1，这种笔误可能性比较小，猜测可能是为了便于阅读和理解，不过我还是不很理解

p在那个地方以那种方式被修改了

``````function next(path1, n)
path = copy(path1)
...
return(path)
end

let
paths1=Paths(3,3)
v1 = []
v2 = []
for p in paths1
push!(v1, p)
push!(v2, copy(p))
end
v1 == v2
end
# 返回值是true.
``````