我想通過在朱莉婭重複一些簡單的ProjectEuler問題來學習朱莉婭。到目前爲止,一切都非常順利,直到我遇到這個令人沮喪的問題。我花了一些時間調試我的代碼,這裏是我的發現: (但願我不是失去了一些東西真的很愚蠢這裏)朱莉婭做一些奇怪的作業
function is_abundant(n::Int) #just a function
return prod(map(x->int((x[1]^(x[2]+1)-1)/(x[1]-1)),factor(n))) > 2 * n
end
abundants=[12] #there should be a better way to initialize an Array
for i=13:28120
if is_abundant(i)
push!(abundants,i)
end
end
le=abundants; #The following lines are the problems
ri=abundants;
d=length(abundants)
println(d)
pop!(le)
shift!(ri)
println(le==ri, " ", endof(ri), " ", endof(abundants))
我得到的輸出是:
6964 true 6962 6962
這意味着Julia已將le
,ri
和abundants
的所有三組更改爲pop!
和shift!
中的每一個命令。我可以用一個愚蠢的額外身份映射來解決這個bug /問題:
le=map(x->x,abundants)
ri=map(x->x,abundants)
現在輸出會改變什麼,我最初的預期:
6964 false 6963 6964
我的問題是,如果這不是一個錯誤,爲什麼Julia保持le
,ri
和abundants
集合之間的等價關係?另外,任何人都可以重現這種行爲?我在Ubuntu 14.04上使用Julia「Version 0.3.0-rc3 + 14(2014-08-13 16:01 UTC)」。
只有一個數組,你只是多次引用它。相關:http://julia.readthedocs.org/en/latest/manual/faq/#i-passed-an-argument-x-to-a-function-modified-it-inside-that-function-but-on -the-外的變量-X-IS-仍然不變,這是爲什麼。 – StefanKarpinski 2014-08-28 04:39:56