在 Netlogo 中将函数作为参数传递 [英] Pass a function as a parameter in Netlogo

查看:67
本文介绍了在 Netlogo 中将函数作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多其他编程语言中,您可以将一个函数作为参数传递给另一个函数,并从该函数内部调用它.

In many other programming languages, you can pass a function as an argument to another function and call it from within the function.

无论如何在 Netlogo 中可以做到这一点?

Is there anyway to do this in 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

这将是一个不错的功能.我正在尝试实现一个抽象的本地搜索算法,这对于传递目标函数等非常有用.

This would be a nice feature. I'm trying to implement an abstract local search algorithm which this would be nice for passing in objective functions and such.

推荐答案

您可以通过创建任务并使用 runresult 执行任务来将函数作为参数传递.

You can pass functions as parameters by creating a task and using the runresult to execute the task.

;; 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 中将函数作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆