涉及问号的高级 netlogo 代码出现问题
Trouble with advanced netlogo code involving a question mark
我正在使用 Netlogo v6.0.4,当我尝试 运行 我在 Stack Overflow 上找到的答案中的示例代码时收到以下错误消息。
Nothing named ? has been defined
在this回答中建议以下netlogo代码作为回答:
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)
。
在 2014 年撰写此答案时,Netlogo v5 正在积极使用,它有一个名为 tasks
的功能,即 lambda 方法。但是在 v6 tasks were replaced by anonymous-procedures.
?
就是这里的人吗?我该如何解决这个错误?
你把它合二为一 - 版本中的 ?
本质上是传递给任务的任何变量的占位符。 foreach
的 5.3 dictionary entry 有一个很好的例子:
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 中的主要语法差异是现在您通过使用 ->
运算符明确说明占位符是什么。因此,与上面完全相同的想法,在 6.0 dictionary entry 中的 foreach
示例中转换为 6.0,看起来像这样:
foreach [1.1 2.2 2.6] [ x -> show (word x " -> " round x) ]
=> 1.1 -> 1
=> 2.2 -> 2
=> 2.6 -> 3
在那里,您可以看到 x
被明确定义为占位符。这确实提高了代码的清晰度——你可以定义占位符,但是你喜欢尽可能清晰和明确——这个(在顶部)示例同样有效:
foreach [ 1.1 2.2 2.6 ] [ round_me -> show (word round_me " -> " round round_me) ]
如果您使用多个列表,请注意您必须用 ( )
包围匿名过程,用 [ ]
包围您的占位符声明 - 例如:
( foreach [ 1 2 3 ] [ 10 20 30 ] [ [ a b ] -> print a * b ] )
如果您正在翻译您的代码示例,那么,您可以只专注于明确说明占位符。它也可能有助于将其分解为组成部分以澄清 - 评论中的更多细节:
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
现在,要按照链接答案中 Nicolas 的示例,您可以调用 to-report
来拆分您的文本:
to read-example
let line "Example\tof\tsome\ttext"
show split2 line "\t"
end
给你:
observer: ["Example" "of" "some" "text"]
希望对您有所帮助!
我正在使用 Netlogo v6.0.4,当我尝试 运行 我在 Stack Overflow 上找到的答案中的示例代码时收到以下错误消息。
Nothing named ? has been defined
在this回答中建议以下netlogo代码作为回答:
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)
。
在 2014 年撰写此答案时,Netlogo v5 正在积极使用,它有一个名为 tasks
的功能,即 lambda 方法。但是在 v6 tasks were replaced by anonymous-procedures.
?
就是这里的人吗?我该如何解决这个错误?
你把它合二为一 - 版本中的 ?
本质上是传递给任务的任何变量的占位符。 foreach
的 5.3 dictionary entry 有一个很好的例子:
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 中的主要语法差异是现在您通过使用 ->
运算符明确说明占位符是什么。因此,与上面完全相同的想法,在 6.0 dictionary entry 中的 foreach
示例中转换为 6.0,看起来像这样:
foreach [1.1 2.2 2.6] [ x -> show (word x " -> " round x) ]
=> 1.1 -> 1
=> 2.2 -> 2
=> 2.6 -> 3
在那里,您可以看到 x
被明确定义为占位符。这确实提高了代码的清晰度——你可以定义占位符,但是你喜欢尽可能清晰和明确——这个(在顶部)示例同样有效:
foreach [ 1.1 2.2 2.6 ] [ round_me -> show (word round_me " -> " round round_me) ]
如果您使用多个列表,请注意您必须用 ( )
包围匿名过程,用 [ ]
包围您的占位符声明 - 例如:
( foreach [ 1 2 3 ] [ 10 20 30 ] [ [ a b ] -> print a * b ] )
如果您正在翻译您的代码示例,那么,您可以只专注于明确说明占位符。它也可能有助于将其分解为组成部分以澄清 - 评论中的更多细节:
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
现在,要按照链接答案中 Nicolas 的示例,您可以调用 to-report
来拆分您的文本:
to read-example
let line "Example\tof\tsome\ttext"
show split2 line "\t"
end
给你:
observer: ["Example" "of" "some" "text"]
希望对您有所帮助!