在朱莉娅,没有内置函数来汇总一组数字?
x = rand(10)
sum(x) # or sum(x, 1)
ERROR: MethodError: objects of type Float64 are not callable
Run Code Online (Sandbox Code Playgroud)
我的意思是我可以写一个for循环来总结如下,
sum = 0.0
for i in 1:length(x)
sum += x[i]
end
Run Code Online (Sandbox Code Playgroud)
但是如果朱莉娅没有把它建在某个地方,我只会感到惊讶吗?
正如@Michael K. Borregaard所提到的那样,你在某个时刻用a的值重新设置了变量sum(默认情况下从中导出Base)Float64.当您重新开始会话时,sum再次是Base.sum默认值,即:
julia> x = rand(10)
10-element Array{Float64,1}:
0.661477
0.275701
0.799444
0.997623
0.731693
0.557694
0.833434
0.90498
0.589537
0.382349
julia> sum
sum (generic function with 16 methods)
julia> sum(x)
6.733930084133119
julia> @which sum(x)
sum(a) in Base at reduce.jl:359
Run Code Online (Sandbox Code Playgroud)
注意警告:
julia> original_sum = sum
sum (generic function with 16 methods)
julia> sum = x[1]
WARNING: imported binding for sum overwritten in module Main
0.6614772171381087
julia> sum(x)
ERROR: MethodError: objects of type Float64 are not callable
julia> sum = original_sum
sum (generic function with 16 methods)
julia> sum(x)
6.733930084133119
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4439 次 |
| 最近记录: |