在许多其他编程语言中,您可以将一个函数作为参数传递给另一个函数并从该函数内调用它。
无论如何,在 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
end
to go
show g f 1 2 3
end
这将是一个很好的功能。我正在尝试实现一个抽象的本地搜索算法,这对于传递目标函数等非常有用。
您可以通过创建任务并使用 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)