netlogo lists or… james says, fput this!. what are lists? shopping list address list dna sequences...

Post on 30-Mar-2015

220 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

NETLOGO LISTS

Or…

James says, “fput THIS!”

What are LISTS?

Shopping listAddress listDNA sequences

Any collection of similar or dissimilar things

Often are ordered in some way

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.

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

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!

Good and Bad Syntax Examples

GOOD[ ]

[ 1 2 3 ]

[ “Hello!”

“I am James!”

]

BAD[ 1, 2, 3, 4 ]

5 4 6

{ “A” “B” “C” }

Creating Lists

Three ways to create lists

Assign the empty list

Assign a list of constants

Assign the results of list constructors

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)

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

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 ]

]

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.

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.

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

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

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 ]

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

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 ]

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 ]

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

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

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)

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?

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.

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)

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

Basic List Tools

ReverseReverses the order of the items in the list

SortSorts the list

ShuffleRandomizes the list

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

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

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

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 ]

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 ]

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 )

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

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!

top related