Help - How to access items from a list and use the member? command?

55 views
Skip to first unread message

Rafaela Lorena

unread,
Oct 4, 2021, 8:56:08 PM10/4/21
to netlogo-users
Hello,

I have two questions, which I think you can help me with.

1) I have a list called patch-visited. This list grows a lot with each interaction
turtles-own [ patch-visited]
set patch-visited ( list patch-here )
set patch-visited lput patch-here patch-visited

I would like, first, to just get the last patch the turtle visited from this patch-visited list and not visit it in the next interaction. For this I used the last command, but it gave the following error: MEMBER? expected input to be a string or list or agentset but got the patch (patch 13 -19) instead.

let availablePatch neighbors with [ not member? self [ last patch-visited ] of myself ]

How can I solve this?

2) The other question is that I searched the NetLogo dictionary and didn't find it. Is it possible for me to access the last 3 or 4 numbers in a list? For example, I can use last to access the last item in a list. But if I want to access the last 3 items in a list, how can I do that?

Thanks in advance!

Charles Staelin

unread,
Oct 5, 2021, 9:25:59 AM10/5/21
to Rafaela Lorena, netlogo-users
Hello Rafaela,

Let's start with question 2.  What you want here is the sublist primitive.  The following procedure will report the last num elements of a list, lst, or, if there are fewer than n elements in the list, the whole list.
to-report get-last [num lst]
  let b length lst
  let a b - num
  report sublist lst (ifelse-value (a < 0) [0] [a]) b
end

observer> show get-last 3 [1 2 3 4 5]
observer: [3 4 5]
observer> show get-last 6 [1 2 3 4 5]
observer: [1 2 3 4 5]
observer> show get-last 3 []
observer: []

As for your first question, In

set patch-visited ( list patch-here )
set patch-visited lput patch-here patch-visited

if these two commands really follow each other, you are first making patch-visited a one element list, so it might look like [(patch 0, 1)], and then adding it to itself, getting something like [(patch 0, 1) (patch 0, 1)].  Putting that aside, however, the last primitive will pull out the last element of that list, (patch 0, 1), which is a patch, not a list.  member?, however, looks for elements in a list so it gives you that error if, instead of looking in a list of patches, it is looking at a single patch.  What you want to do is compare self, a patch, with the last element in patch-visited, a patch, using  !=.

let availablePatch neighbors with [ self != [ last patch-visited ] of myself ]

Or, you could put both your questions together and use

let availablePatch neighbors with [not member? self [get-last 1 patch-visited] of myself]

Since get-last returns a list of patches, even if it contains only a single patch, member? will work on it.  And if you want to exclude, say the three most recent patches, simply replace 1 by 3.

Hope this helps,
Charles
-------------------------------
Charles P. Staelin
Professor Emeritus of Economics
Smith College
Northampton, MA 01063


--
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/CAHs8kB95tizuD45UgnnhZEM24BRCmka8Qd%2B7atWeTzPna-zLHg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages