让我们建立一个简单的例子,其中每个龟使得在每个替随机角度稍微向右转,并存储这些角度的历史列表:
turtles-own [ history ]
to setup
clear-all
create-turtles 3 [
set history [] ; initialize history to an empty list
]
reset-ticks
end
to go
ask turtles [
let angle random 5
right angle
; add the angle of the most recent turn
; at the end of the list
set history lput angle history
if length history > 10 [
; remove the first item from the list
set history but-first history
]
forward 0.1
]
tick
end
我不想要一个类似于单元格列表的列表,除非有一些简单的方法可以将数学运算应用于我不知道的列表中的每个项目。
我不知道你是什么意思的“一个缺点列表”,但像我们这里建立的一个简单的列表是目前为止你可以使用的最好的东西,如果你想要做的数学。
要应用操作的每个项目,使用map
。然后你可以使用像sum
或mean
这样的功能来操作整个列表。例如:
to do-math
ask turtles [
let doubled-history map [ a -> a * 2 ] history
show history
show doubled-history
show mean doubled-history
]
end
(注意,这里使用的NetLogo 6.0.1语法)。
让我们来看看一个示范:
observer> setup repeat 5 [ go ] do-math
(turtle 2): [4 3 2 1 2]
(turtle 2): [8 6 4 2 4]
(turtle 2): 4.8
(turtle 1): [4 0 1 1 4]
(turtle 1): [8 0 2 2 8]
(turtle 1): 4
(turtle 0): [2 0 4 1 0]
(turtle 0): [4 0 8 2 0]
(turtle 0): 2.8
包括你已经写了一些代码,并说明不工作。 –