Loop in NetLogo - doubts

214 views
Skip to first unread message

Karina Santos

unread,
Jul 27, 2022, 11:41:54 AM7/27/22
to netlogo-users
Hi all, how are you?

I have the following question about loop in NetLogo:

What replaces the "for" command used for example in R in NetLogo language? Could it be foreach?

Loop types in NetLogo, we can use the following commands: repeat, if, ifelse, while, loop and foreach? Correct?

Thank you

James Steiner

unread,
Jul 27, 2022, 2:31:27 PM7/27/22
to Karina Santos, netlogo-users

There are actually secret looping commands. Secret because many people don't think of them as looping. 

Because netlogo is designed to make it easy to work with quantities of agents, it has built in way to loop through agents in sets and in lists. 

ASK -- loops through the agents and makes them do stuff

(Like foreach ( agent in agents ), and self is like this)

OF -- loops through a set of agents And make them perform a calculation, returns a list of the results

MAP -- loops through a list, performing a calculation on each list item, returns a list of the results 

REDUCE - loops through a list, combining the items using some expression, returns a single value

WITH - loops through a set of agents, making them perform a true/false 👍🏽calculation. Returns the set of agents that returned true 

CREATE, SPROUT, HATCH -- creates a number of agents, then loops through the agents making them do stuff. 






--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/CABCtG4FAo1MGjBzC61QkB7XrtxcVAPMOqTM6CiseJj-3nRjSWg%40mail.gmail.com.

Wade Schuette

unread,
Jul 27, 2022, 2:48:49 PM7/27/22
to netlogo-users
Cool, James. I forgot there was "loop".
And here's the inelegant but functional mapping of "for" to NetLogo
;;=========================
to setup
clear-all
  let startval 3
  let maxval 9
  let stepval 2
 
  ;; equivalent to: for ( i = startval; i <= maxval; i = i + stepval)
  let i startval
  while [ i <= maxval ]
  [
    print ( word "looping from " startval " to "  maxval " by "  stepval ", now at i = " i )
    set i ( i + stepval )
  ]
 
end

Reply all
Reply to author
Forward
0 new messages