我在将旧的NetLogo代码转换为可与NetLogo 6.0.1一起使用时遇到问题
我遇到问题的行如下:
set recv turtle (read-from-string ?)
我知道吗?已更改为->。但我不知道这条线是如何工作的。
整个代码是:
to send [msg]
let recipients get-receivers msg
let recv 0
foreach recipients [
set recv turtle (read-from-string ?)
if recv != nobody [without-interruption [ask recv [receive msg]]] ;; read-from-string is required to convert the string to number
]
end
[?
代表recipients
的每个元素,现在被foreach
括号内声明的局部变量代替。在这里,我已经声明了一个变量recip
,但是它可以是您想要的任何东西。
to send [msg]
let recipients get-receivers msg
let recv 0
foreach recipients [recip ->
set recv turtle (read-from-string recip)
if recv != nobody [without-interruption [ask recv [receive msg]]] ;; read-from-string is required to convert the string to number
]
end
关于此的两点好处:您可以为问号“命名”,从而更容易记住它的含义,并且,如果要遍历一个以上的列表,则可以声明一个以上的变量,每个变量一个列表。
查尔斯