How can i make a list of cumulative sum of a other list?
i tried it that way:
;;all temperatrue-values around the turtle saved in list
set temperature_values (list [(output-heat + 1)^ Freedom] of neighbors)
;;build cumulative value of temperatures and put each value in list
let tempsum 0
set tempsum_list []
foreach temperature_values
[set tempsum (tempsum + ? )
set tempsum_list fput tempsum tempsum_list
]
but it doesn't work. can anyone fix this problem? it says that "+ excepted a input but gets a list instead".
This is like Alan's solution, just abstracted a bit further. (Perhaps too far, depending on your taste! I like JenB's solution as well.)
Let's first define a thing like
reduce
, but that keeps all the intermediate results:Now we can use it to compute partial sums:
but we are also free to swap in a different operation:
Similar to Alan's solution (Just an update for the recent version of NetLogo that replaces ? with -> for anonymous procedures.)
your code for a cumulative sum works (except that I think you need
lput
rather thanfput
. You can see it with this:Did the error highlight the line
set temperature_values (list [(output-heat + 1)^ Freedom] of neighbors)
? Try putting a space after between ) and ^. NetLogo is picky about space around mathematical operators.As Jen suggested, you can use
foreach
. Another nice approach is reduce: