![Page 1: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/1.jpg)
NETLOGO LISTS
Or…
James says, “fput THIS!”
![Page 2: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/2.jpg)
What are LISTS?
Shopping listAddress listDNA sequences
Any collection of similar or dissimilar things
Often are ordered in some way
![Page 3: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/3.jpg)
What are LISTS?
A list is a variable that contains a list of values.
Lists can contain any type of value, including other lists.
Lists can contain different types of values at once.
![Page 4: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/4.jpg)
Examples of NetLogo Lists
[ ][ 1 2 3 99 -2.9 ][ “a” “sam” ][ [ -1 0 ] [ 1 1 ] [ 1 -1 ] ][ “g” “a” “c” “t” ][ 12 “bob” [ 3 12 4 ] ]
![Page 5: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/5.jpg)
List Syntax
Zero or more values surrounded by square brackets [ ]
Values are separated by “white space”
Values are NOT separated by commas, or any other punctuation!
![Page 6: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/6.jpg)
Good and Bad Syntax Examples
GOOD[ ]
[ 1 2 3 ]
[ “Hello!”
“I am James!”
]
BAD[ 1, 2, 3, 4 ]
5 4 6
{ “A” “B” “C” }
![Page 7: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/7.jpg)
Creating Lists
Three ways to create lists
Assign the empty list
Assign a list of constants
Assign the results of list constructors
![Page 8: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/8.jpg)
The Empty List – [ ]
Assigning [ ] to a variable makes it an empty list.; erase turtle’s memory
set memory [ ]
; memory is now an empty list
(likewise, “” creates an empty string)
![Page 9: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/9.jpg)
Lists of Constants – Part 1
set friends [ “Bob” 25 “Mary” 41 ]
set my-rgb [ .5 .75 .1 ]
set dna [ “c” “t” “a” “g” “a” “g” “t” “t” ]
![Page 10: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/10.jpg)
Lists of Constants – Part 2
Lists of Lists of constantsset successes [ [ 7 “B”] [ 8 7 “Z” ] [ 3 ] ]set matrix [ [ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]
]
![Page 11: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/11.jpg)
Lists of Constants – Part 3
For some reason, this only works with constants, not variables.
So, this will not work:
set my-list [ heading xcor ycor ]
Fortunately, there is a way to build lists with variables.
![Page 12: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/12.jpg)
List Constructors
There are many primitive reporters that report a list.
Some create a list from other things.
Some take a list as input, and can be used to modify lists.
![Page 13: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/13.jpg)
List Constructor Primitives
but-first but-last filter fput list lput map modes n-values
remove remove-duplicates remove-item replace-item reverse sentence shuffle sort, sort-by values-from
![Page 14: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/14.jpg)
Two Words About Strings
Lists and Strings share some primitives.
…however…
A string is not a list of characters.
String: “bob”List: [ “b” “o” “b” ]
![Page 15: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/15.jpg)
List – The Fresh List Maker
Syntax: list {item1} {item2}reports a list containing the two inputs
print list thing other-thingO> [ -2 5 ]
Print list (xcor * 2) colorT> [ 10.0 9.9999 ]
![Page 16: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/16.jpg)
List – Code-Fu
Wrap list and the items in ( ) to create lists with any number of items at once.print (list xcor heading label)
T> [ -2.0 322.43 “walrus” ]print (list population) ; one item
O> [ 10000 ]print (list ) ; zero items
O> [ ]
![Page 17: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/17.jpg)
Values-From – Agent Sucker
values-from {agentset} [ {reporter} ]
makes a list of values calculated by an agentset
print values-from turtles [ heading ]
O> [ 45 90 135 ]
![Page 18: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/18.jpg)
Values-From
used often for aggregating agent data print sum values-from turtles [ net-worth ]
print mean values-from neighbors [ age ]
used to load an agentset into a listset patch-list values-from patches [ self ]
![Page 19: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/19.jpg)
fput – lput
lput {value} {list} ; (last)fput {value} {list} ; (first)use to build up lists one item at a timereports a list that is the given list with the
value appended, becoming the new last or first item. ; add this taste to memory list
set memory lput taste memory
![Page 20: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/20.jpg)
but-first – but-last
but-first {list} but-last {list}
reports the result of removing the first or last item from the given listprint but-first [ “bird” “cat” “dog” ]
O> [ “cat” “dog” ]
![Page 21: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/21.jpg)
Combining fput & but-last
Combine these to update a “constant-length” list ; add new memory, forget oldest memory
set memory fput taste ( but-last memory )
Likewise, lput and but-first set queue but-first (lput customer
queue)
![Page 22: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/22.jpg)
Item – the List Interrogator
item {index} {list}items in a list are indexed by numberthe first list item has an index of zerothe “item” primitive returns the value of
an item in a list at the given indexset info [ “a” “b” “huh?” “bob” ]
print item 2 info
O> huh?
![Page 23: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/23.jpg)
REMEMBER!
List-making primitives are reporters!Primitives that “modify” a list don’t
actually change the input list, but report a result based on the list.
So, to modify a list, the result must be assigned back to the input list.
![Page 24: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/24.jpg)
Using Lists - Length
Length reports the number of items in the list.
Watch out for using Count when you want to use Length!
count is for agentsetslength is for lists (and strings)
![Page 25: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/25.jpg)
Using Lists – First, Last
Shorthand to get the first or last elements of a list
First returns the first list item. ( first my-list ) same as (item 0 my-list )
Last returns the last item ( last my-list ) same as
( item ( ( length my-list ) - 1 ) my-list
![Page 26: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/26.jpg)
Basic List Tools
ReverseReverses the order of the items in the list
SortSorts the list
ShuffleRandomizes the list
![Page 27: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/27.jpg)
LISTS Code-Fu
These list primitives are powerful tools for manipulating lists.
They are the amateur programmer’s friend. (pro programmers, too)
They let us easily Convert, Combine, Reduce, Analyze, Fold, Spindle, and Mutilate lists
Without these, you’d need to write some rather complicated code to achieve the same results
![Page 28: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/28.jpg)
LISTS Code-Fu - MAP
map [ {reporter} ] {list}( map [ {reporter} ] {list1} {list2} {…} )
Map lets us perform the same operation on every item of a list, creating a new list with the results
![Page 29: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/29.jpg)
LISTS Code-Fu - MAP
Double the value of each item in the list
The following two slides show how to perform a simple task on every item in a list
Both sets of code use the following assumptions: (which you don’t need to memorize…this is only FYI)
the variable original-list exists the variable index exists the variable modified-list exists
![Page 30: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/30.jpg)
LISTS Code-Fu - MAP
Double the value of each item in the list The “hard” way, without map
set original-list [ 1 2 3 4 ]
set index 0set modified-list [ ]repeat ( length original-list )[ set modified-list (lput((item index original-list)* 2)) set index index + 1]print original-list + “ x 2 = “ + modified-listO> [ 1 2 3 4 ] x 2 = [ 2 4 6 8 ]
![Page 31: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/31.jpg)
LISTS Code-Fu - MAP
Double the value of each item in the list The “easy” way, with map
set original-list [ 1 2 3 4 ]
set modified-list map [ ? * 2 ] original-list
print original-list + “ x 2 = “ + modified-listO> [ 1 2 3 4 ] x 2 = [ 2 4 6 8 ]
![Page 32: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/32.jpg)
LISTS Code-Fu - MAP
Add two lists, making a third list set income [ 100 101 54 242 ]
set outgo [ 123 99 75 99 ]
set net-worth ( map [ ?1 - ?2 ] income outgo )
![Page 33: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/33.jpg)
Things to look at more:
FilterCreates a list by selecting the items in the
given list that match the criteriaReduce
Reduces a list to a single value, using the formula you specify
ForeachLike MAP, but lets you run code on the list
items, rather than making a new list
![Page 34: NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often](https://reader034.vdocument.in/reader034/viewer/2022042623/5518cfff550346991f8b5c47/html5/thumbnails/34.jpg)
LIST Ideas
Turtle or Patch Memory Choices made Patches visited State history Group members
Arrays of properties DNA, “Genes” Preset information
Plot data Lines read from files Undo history Route history Queues Stacks Matrices You Name It!