试图欺骗NetLogo执行作为参数传递的函数 [英] Attempting to trick NetLogo into executing a function passed as an argument
问题描述
以下内容会产生编译错误.
The following produces a compilation error.
to-report call-first/last [first/last a-list]
report first/last a-list
end
正文中的
a-list
突出显示,并显示错误: Expected command.
a-list
in the body is highlighted with the error: Expected command.
所以我尝试了以下方法.
So I tried the following.
to-report call-first/last [first/last a-list]
report first map first/last (list a-list)
end
此代码已编译(!),但是当我尝试让观察者执行它时
This code compiled(!), but when I attempted to have the observer execute it
call-first/last first [1 2 3]
呼叫优先/最后
突出显示,并显示错误消息: CALL-FIRST/LAST预期有2个输入.
call-first/last
was highlighted with the error message: CALL-FIRST/LAST expected 2 inputs.
对替代方法有何想法?
谢谢.
P.S.更大的前景是找到一种方法(如果有的话)在NetLogo中编写一个高阶函数.
P.S. The bigger picture is to find a way (if there is one) to write a higher-order function in NetLogo.
更新:
要求观察者正确执行以下工作!
Asking the observer to execute the following works properly!
通话优先[[a-list]->第一个a-list] [1 2 3]
;;=>1
call-first-last [[a-list] -> first a-list] [1 2 3]
;; => 1
通话优先[[a-list]->最后一个列表] [1 2 3]
;;=>3
call-first-last [[a-list] -> last a-list] [1 2 3]
;; => 3
但是肯定还有很长的路要走!
But it sure is a long way around!
另一个更新:
尝试
to-report call-first-last [first-last a-list]
report first map (runresult first-last) (list a-list)
end
已编译.
但是要求观察者运行 call-first-last"first"命令.[1 2 3]
产生了运行时错误:首先需要1个输入
But asking the observer to run call-first-last "first" [1 2 3]
produced a Runtime Error: FIRST expected 1 input
所以问题似乎归结为是否存在一种方法(除了使用上述匿名表达式之外)来编写返回函数的表达式?
So the question seems to come down to whether there is a way (other than using an anonymous expression as above) to write an expression that returns a function?
推荐答案
我不确定所有细节是否正确,但是当我看到您要执行的操作时,我的第一个想法就是使用 runresult
.这有效:
I'm not sure I have all the details correct, but my first thought when I saw what you were trying to do is to use runresult
. This works:
to-report call-first-last [first-last a-list]
report runresult word first-last a-list
end
使用(例如):call-first-last"last"[1 2 3]
With (for example): call-first-last "last" [1 2 3]
当您使用两个以上的术语来编写更复杂的表达式时,您将需要为 word
和 runresult
添加一些括号.
You would need to add some brackets for both word
and runresult
as you do more complicated expressions with more than two terms.
这篇关于试图欺骗NetLogo执行作为参数传递的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!