Home > Software design >  Using foreach and map to iterate over lists in Netlogo
Using foreach and map to iterate over lists in Netlogo

Time:06-23

I just started learning netlogo a couple of days ago and would appreciate some help. I have a list called "distance", which gives me the distance from each patch in a given radius to a current patch. I want to plug in each value of "distance" into the following, to get an "attractiveness" value for each patch:

Patch attractiveness = exp ((distance / 800) * (log e (.2 / 1)))]

I would like to store these values in another list called "patch_attractiveness." I think I should use either map or foreach, but I do get the error "Nothing named d has been defined." Below is what I have tried:

let Patch_attractiveness map [d -> distance] exp ((d / 800) * (log e (.2 / 1)))]

I think my confusion is primarily with how to use anonymous procedures.

CodePudding user response:

The problem was where you placed distance and the fact that you have two closing brackets but only 1 opening bracket. When using map, the order goes; map [anonymous procedure] list

let Patch_attractiveness map  [d -> exp ((d / 800) * (log e (.2 / 1)))] distance

Foreach is a bit different in the order and also requires some extra steps to make the list you need. For foreach, it goes: foreach list [anonymous procedure]. Foreach does not return a list so you need to make that one yourself.

let Patch_attractiveness []
foreach distance [d -> 
   let this-attractiveness exp ((d / 800) * (log e (.2 / 1)))
   set Patch-attractiveness lput this-attractiveness Patch-attractiveness
] 

Anonymous procedures can feel a bit weird so I encourage you to just keep playing around with them to get used to the syntax.

  • Related