Home > Net >  How do I remove-item from list in turtle attributes?
How do I remove-item from list in turtle attributes?

Time:07-07

I am trying to implement turtles to move along the shortest path. To do this, I want to incrementally remove the first item from the turtles path (which is a list that is part of it's attributes). Below I have presented to simplest version of how I which to do this in a picture of my current model. The question: why do I get the 'expected command' error here? According to the documentation I am using the remove-item function correctly...

enter image description here

CodePudding user response:

ask walker 30 [set path remove-item 0 path] is indeed the proper syntax. remove-item 0 path is not. remove-item returns a new list and you must tell NetLogo where to "put" the list that is returned. set path remove-item 0 path tells NetLogo to replace the old path with the new one. Without the set path part, NetLogo doesn't know what to do with the new list.

Actually, this error is quite common for those of us used to languages that allow mutable objects. We might assume replace-item 0 path would simply take the old path list, drop the first item, and that path would thereafter refer to the new list. But, in NetLogo most objects are immutable and it does not allow these items to be changed in place. NetLogo rather requires that the changed (and therefore new) item be assigned to some variable. In this case, you want it to replace the old path, and that is what set path ... does.

By the way, a more economical way of dropping the first item of a list is to use but-first, as in set path but-first path. but-last is useful for stripping the last item.

  • Related