Function 在Netlogo中将函数作为参数传递
在许多其他编程语言中,您可以将一个函数作为参数传递给另一个函数,并从函数中调用它 Netlogo中是否有这样做的方法 例如:Function 在Netlogo中将函数作为参数传递,function,netlogo,Function,Netlogo,在许多其他编程语言中,您可以将一个函数作为参数传递给另一个函数,并从函数中调用它 Netlogo中是否有这样做的方法 例如: ;; x,y,z are all ints to-report f [x y z] report x + y + z end ;; some-function is a function ;; x y and z are ints to-report g [some-function x y z] report (some-function x y z) + 2
;; x,y,z are all ints
to-report f [x y z]
report x + y + z
end
;; some-function is a function
;; x y and z are ints
to-report g [some-function x y z]
report (some-function x y z) + 2
end
to go
show g f 1 2 3
end
这将是一个很好的功能。我正在尝试实现一个抽象的局部搜索算法,这对于传递目标函数等很好。您不能将函数作为函数传递(我相信),但您当然可以将函数名作为文本传递,然后使用
runresult
原语来运行函数。凌乱但可行。通过创建任务并使用runresult执行任务,可以将函数作为参数传递
;; x,y,z are all ints
to-report f [x y z]
report x + y + z
end
;; some-function is a function
;; x y and z are ints
to-report g [some-function x y z]
report (runresult some-function x y (z + 2))
end
to go
show g (task f) 1 2 3
end
从Netlogo 6.0.1开始,箭头语法取代了任务。下面的操作与接受的答案相同,但使用了更新的语法
to-report f [x y z]
report x + y + z
end
;; some-function is a function
;; x y and z are ints
to-report g [some-function x y z]
report (runresult some-function x y (z + 2))
end
to go
show g [[x y z] -> (f x y z)] 1 2 3
end
谢谢你的建议;然而,在你的帮助下,我找到了解决办法。您不应该在字符串上运行结果,而是可以将函数转换为任务,并使用更好的适当输入在任务上运行结果。是的。有关任务的详细信息,请参阅