Presentation is loading. Please wait.

Presentation is loading. Please wait.

NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often.

Similar presentations


Presentation on theme: "NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often."— Presentation transcript:

1 NETLOGO LISTS Or… James says, fput THIS!

2 What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often are ordered in some way

3 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.

4 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 ] ]

5 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!

6 Good and Bad Syntax Examples GOOD [ ] [ 1 2 3 ] [ Hello! I am James! ] BAD [ 1, 2, 3, 4 ] 5 4 6 { A B C }

7 Creating Lists Three ways to create lists Assign the empty list Assign a list of constants Assign the results of list constructors

8 The Empty List – [ ] Assigning [ ] to a variable makes it an empty list. ; erase turtles memory set memory [ ] ; memory is now an empty list (likewise, creates an empty string)

9 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 ]

10 Lists of Constants – Part 2 Lists of Lists of constants set successes [ [ 7 B] [ 8 7 Z ] [ 3 ] ] set matrix[ [ 1 0 0 ] [ 0 1 0 ] [ 0 0 1 ] ]

11 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.

12 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.

13 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

14 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 ]

15 List – The Fresh List Maker Syntax: list {item1} {item2} reports a list containing the two inputs print list thing other-thing O> [ -2 5 ] Print list (xcor * 2) color T> [ 10.0 9.9999 ]

16 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> [ ]

17 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 ]

18 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 list set patch-list values-from patches [ self ]

19 fput – lput lput {value} {list} ; (last) fput {value} {list} ; (first) use to build up lists one item at a time reports 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

20 but-first – but-last but-first {list}but-last {list} reports the result of removing the first or last item from the given list print but-first [ bird cat dog ] O> [ cat dog ]

21 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)

22 Item – the List Interrogator item {index} {list} items in a list are indexed by number the first list item has an index of zero the item primitive returns the value of an item in a list at the given index set info [ a b huh? bob ] print item 2 info O> huh?

23 REMEMBER! List-making primitives are reporters! Primitives that modify a list dont 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.

24 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 agentsets length is for lists (and strings)

25 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

26 Basic List Tools Reverse Reverses the order of the items in the list Sort Sorts the list Shuffle Randomizes the list

27 LISTS Code-Fu These list primitives are powerful tools for manipulating lists. They are the amateur programmers friend. (pro programmers, too) They let us easily Convert, Combine, Reduce, Analyze, Fold, Spindle, and Mutilate lists Without these, youd need to write some rather complicated code to achieve the same results

28 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

29 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 dont need to memorize…this is only FYI) the variable original-list exists the variable index exists the variable modified-list exists

30 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 0 set 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-list O> [ 1 2 3 4 ] x 2 = [ 2 4 6 8 ]

31 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-list O> [ 1 2 3 4 ] x 2 = [ 2 4 6 8 ]

32 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 )

33 Things to look at more: Filter Creates a list by selecting the items in the given list that match the criteria Reduce Reduces a list to a single value, using the formula you specify Foreach Like MAP, but lets you run code on the list items, rather than making a new list

34 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!


Download ppt "NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often."

Similar presentations


Ads by Google