1

When I try the following command on NL 6.1.1:

(map + [1 2 3] [2 4 6] [7 8 9])

I get [3 6 9]as a result instead of [10 14 18], which means that only the first two lists are considered in the sum.

Anyone knows how to correct this and get all three lists to be considered ?

lomper
  • 379
  • 1
  • 12

1 Answers1

4

Per the documentation in the NetLogo dictionary, the map reporter does support multiple lists, but map expects the provided anonymous reporter to take a number of arguments equal to the number of lists you provide. In your case you cannot use the + shortcut since + takes 2 arguments, and we have 3 lists.

So instead you can do this:

(map [ [a b c] -> a + b + c ] [1 2 3] [2 4 6] [7 8 9])

If you need to handle adding an arbitrary number of lists (as in a list variable that could have 2, 3, 4, or more lists inside of it depending on your program), you can try this out using the reduce reporter:

set vals [[1 2 3] [2 4 6] [7 8 9] [10 11 12]]
reduce [ [a b] -> (map + a b) ] vals
Jasper
  • 2,610
  • 14
  • 19
  • Thanks @Jasper, that is exactly what I was trying to get at, since I have a variable-length list of 3-dimensional vectors. I don't entirely understand the syntax but I see it works. I will study it a bit more. Cheers! – lomper Nov 19 '19 at 15:31
  • 1
    Glad I included the second one! One note is that the `reduce` will not work with length 0 lists, so you'll need to handle that case separately: `ifelse-value length vals = 0 [0] [reduce [ [a b] -> (map + a b) ] vals]` – Jasper Nov 19 '19 at 16:02