Miguel Pais
Miguel Pais

Reputation: 65

NetLogo 6 anonymous procedures with no inputs

In the programming guide it's mentioned that anonymous procedures can have no inputs:

[[] -> fd 1]
[[] -> count turtles]

My question is what would be the use of such a procedure?

Upvotes: 4

Views: 209

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

You'll see a few examples in the NetLogo Models Library if you run grep -rl "\[\] ->" from the models folder, namely:

Code Examples/Extensions Examples/nw/NW General Examples.nlogo
Code Examples/State Machine Example.nlogo
3D/Sample Models/Termites 3D.nlogo3d
Sample Models/Biology/BeeSmart Hive Finding.nlogo
Sample Models/Chemistry & Physics/Sandpile.nlogo
Sample Models/Social Science/Hotelling's Law.nlogo
IABM Textbook/chapter 8/Sandpile Simple.nlogo

I would encourage you to take a look at them, but a common use is to store a task that you want a turtle to execute later.

If you take the State Machine Example, you'll see that termites have a next-task variable that gets set to different anonymous procedures depending on what the termite should be doing next. For example, when a termite stumbles upon a pile of chips, its next task will be to put down the chip it is currently holding:

to find-new-pile  ;; turtle procedure -- look for yellow patches
  if pcolor = yellow
    [ set next-task [ [] -> put-down-chip ] ]
end

You don't always need this technique, but it's a powerful tool for modeling certain kind of systems (e.g., state machines).

Upvotes: 3

Related Questions