将 运行 与匿名过程一起使用

Using run with anonymous procedures

我是 netlogo 的新手,我无法理解与使用 运行 匿名过程相关的性能问题。

我主要担心的是,使用 运行 或 运行 匿名过程的结果是否会导致性能下降,或者这是否与仅对字符串输入使用 run/runresult 有关。

谢谢

一些代码:

我的理解是,您可以通过以下 两种 不同的方式获得相同的结果(可能还有更多,但正如我所说,我只是从 netlogo 开始,所以这些是我能想到的):

to-report list-made-up-of-empty-lists? [ a-list ]

  let helper [ [ arg ] -> ( reduce and ( map empty? arg ) )]

  report ( map helper ( list a-list ) )

end

to-report list-made-up-of-empty-lists? [ a-list ]

  let helper [ [ arg ] -> ( reduce and ( map empty? arg ) )]

  report ( list ( runresult helper a-list ) )

end

有什么区别?

此外,在文档中指出 runresult ( word "helper " a-list ) 之类的东西原则上应该可以工作,但我不能做到 运行(我得到 运行 时间错误)。

这最后一行不应该正确评估吗?我究竟做错了什么?另外,在什么意义上可以 runresult '运行' strings?

执行匿名过程比 运行 字符串快得多。问题是字符串需要转换成一些可执行代码,然后执行。

如果您将用户定义的函数与匿名过程进行比较,那是另一回事并且取决于用例。例如,如果你有一个 for 循环并在循环内创建一个匿名函数,而不是在循环外创建它(或预先定义它),你可能会开始看到速度变慢。

Netlogo 的 运行 文档:

run command  
(run command input1 ...)  
run string  
runresult reporter 
(runresult reporter input1 ...)  
runresult string  

The run form expects the name of a command, an anonymous command, or a string containing commands. This agent then runs them.

The runresult form expects the name of a reporter, an anonymous reporter, or a string containing a reporter. This agent runs it and reports the result.

此外,您的代码无法使用字符串的原因如下:Netlogo 的字符串命令不能 set/read 局部变量。你的助手是一个局部变量。

查看文档:

Anonymous procedures may freely read and/or set local variables and procedure inputs. Trying to do the same with strings may or may not work and should not be relied on.