涉及问号的高级netlogo代码出现问题
我使用的是Netlogo v6.0.4,当我试图从堆栈溢出中找到的答案运行示例代码时,会收到以下错误消息 没有名字吗?已定义 在回答中,建议使用以下netlogo代码作为回答:涉及问号的高级netlogo代码出现问题,netlogo,Netlogo,我使用的是Netlogo v6.0.4,当我试图从堆栈溢出中找到的答案运行示例代码时,会收到以下错误消息 没有名字吗?已定义 在回答中,建议使用以下netlogo代码作为回答: to-report split [ string delim ] report reduce [ ifelse-value (?2 = delim) [ lput "" ?1 ] [ lput word last ?1 ?2 but-last ?1 ] ] fp
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
它喜欢的特定?
是本节子字符串字符串中的第一个?(?+1)
当这个答案在2014年编写时,Netlogo v5正在积极使用,它有一个名为任务
的功能,这是lambda方法。但是在
这就是
?
在这里的内容吗?如何修复此错误?您在一个版本中得到了它-版本中的?
实际上是传递给任务的任何变量的占位符。forforeach
有一个很好的例子:
foreach [1.1 2.2 2.6] [ show (word ? " -> " round ?) ]
=> 1.1 -> 1
=> 2.2 -> 2
=> 2.6 -> 3
在这种情况下,foreach
获取了[1.1 2.2.6]
的输入列表并对其进行迭代,其中?
位于正在处理的当前项的命令块中。据我所知,6.X在语法上的主要区别在于,现在通过使用->
操作符显式声明占位符是什么。因此,在中的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 read-example
let line "Example\tof\tsome\ttext"
show split2 line "\t"
end
给你:
observer: ["Example" "of" "some" "text"]
希望这是有帮助的 您在一个版本中得到了它-版本中的?
实际上是传递给任务的变量的占位符。forforeach
有一个很好的例子:
foreach [1.1 2.2 2.6] [ show (word ? " -> " round ?) ]
=> 1.1 -> 1
=> 2.2 -> 2
=> 2.6 -> 3
在这种情况下,foreach
获取了[1.1 2.2.6]
的输入列表并对其进行迭代,其中?
位于正在处理的当前项的命令块中。据我所知,6.X在语法上的主要区别在于,现在通过使用->
操作符显式声明占位符是什么。因此,在中的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 read-example
let line "Example\tof\tsome\ttext"
show split2 line "\t"
end
给你:
observer: ["Example" "of" "some" "text"]
希望这是有帮助的 回答得很好。我把它从网上链接了起来。回答得很好。我把它从一个地方交叉连接起来。