带有问号的高级netlogo代码出现问题 [英] Trouble with advanced netlogo code involving a question mark
问题描述
我正在使用Netlogo v6.0.4,当我尝试从我在Stack Overflow上找到的答案中运行示例代码时,收到以下错误消息.
I am using Netlogo v6.0.4 and I'm getting the following error message when I try to run sample code from an answer I found here on Stack Overflow.
没有名字吗?已定义
Nothing named ? has been defined
在此答案中,提出了以下netlogo代码作为答案:
In this answer the following netlogo code is proposed as an answer:
to-report split [ string delim ]
report reduce [
ifelse-value (?2 = delim)
[ lput "" ?1 ]
[ lput word last ?1 ?2 but-last ?1 ]
] fput [""] n-values (length string) [ substring string ? (? + 1) ]
end
它喜欢的特定?
是本节substring string ? (? + 1)
中的第一个.
The specific ?
it does like is the first one in this section substring string ? (? + 1)
.
在2014年撰写此答案时,Netlogo v5一直在积极使用,它具有称为tasks
的功能,它们是lambda方法.但是在 v6任务被匿名程序代替.
When this answer was written in 2014, Netlogo v5 was in active use and it had a feature called tasks
that were lambda methods. But in v6 tasks were replaced by anonymous-procedures.
推荐答案
您一目了然-版本中的?
本质上是传递给任务的变量的占位符. foreach
的 5.3字典条目具有一个很好的例子:
You got it in one- the ?
in the versions were essentially placeholders for whatever variable was being passed to the task. The 5.3 dictionary entry for foreach
has a good example:
foreach [1.1 2.2 2.6] [ show (word ? " -> " round ?) ]
=> 1.1 -> 1
=> 2.2 -> 2
=> 2.6 -> 3
在这种情况下,foreach
正在获取[1.1 2.2 2.6]
的输入列表并对其进行迭代,其中?
在正在处理的当前项目的命令块中占位.据我了解,6.X的主要语法差异在于,现在您可以使用->
运算符明确声明该占位符是什么.因此,与上述完全相同的想法在
In that case, foreach
was taking the input list of [1.1 2.2 2.6]
and iterating over it, where the ?
takes the place in the command block of the current item being processed. As I understand it, the main syntactical difference in 6.X is that now you explicitly state what that placeholder is, by using the ->
operator. So, the exact same idea as above, translated to 6.0 in the foreach
example in the 6.0 dictionary entry, looks like this:
foreach [1.1 2.2 2.6] [ x -> show (word x " -> " round x) ]
=> 1.1 -> 1
=> 2.2 -> 2
=> 2.6 -> 3
在那里,您可以看到x
被明确定义为占位符.这确实提高了代码的清晰度-您可以定义占位符,但是您希望根据自己的意愿使之清晰明了-此(上方)示例也同样有效:
There, you can see that the x
is explicitly being defined as the placeholder. This really improves the clarity of code- you can define the placeholder however you like to be as clear and explicit as you'd like- this (over the top) example works just as well:
foreach [ 1.1 2.2 2.6 ] [ round_me -> show (word round_me " -> " round round_me) ]
如果要使用多个列表,请注意,匿名程序必须用( )
括起来,占位符声明用[ ]
-括起来,例如:
If you're using multiple lists, do note that you have to surround the anonymous procedure with ( )
and your placeholder declaration with [ ]
- for example:
( foreach [ 1 2 3 ] [ 10 20 30 ] [ [ a b ] -> print a * b ] )
然后,如果要翻译代码示例,则可以专注于显式声明占位符.它也可能有助于将其分解为各个组成部分以澄清-注释中的更多详细信息:
If you're translating your code example, then, you can just focus on explicitly stating the placeholders. It also might help to break it down into the component parts to clarify- more detail in comments:
to-report split2 [ string delim ]
; split the string up into individual characters
let characters fput [""] n-values ( length string ) [ a -> substring string a ( a + 1 ) ]
; build words, cutting where the delimiter occurs
let output reduce [ [ b c ] ->
ifelse-value ( c = delim )
[ lput "" b ]
[ lput word last b c but-last b ]
] characters
report output
end
现在,要在链接的答案中遵循尼古拉斯的例子,您可以调用to-report
拆分文本:
Now, to follow Nicolas' example from your linked answer, you can call that to-report
to split up your text:
to read-example
let line "Example\tof\tsome\ttext"
show split2 line "\t"
end
给你:
observer: ["Example" "of" "some" "text"]
希望有帮助!
这篇关于带有问号的高级netlogo代码出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!