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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1.
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Hash Tables and Sets Lecture 3. Sets A set is simply a collection of elements Unlike lists, elements are not ordered Very abstract, general concept with.
Container Types in Python
1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Arrays Strings and regular expressions Basic PHP Syntax CS380 1.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
Sixteen lists and compound data. Recap Names: constants and variables When evaluated, return a specific data objects Can make new names with: [define.
Fourteen lists and compound data. Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, , etc. “Floating-point” numbers ,
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
C++ fundamentals.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Lists in Python.
Week 11 DO NOW QUESTIONS. An ask turtles block is a set of instructions that is issued to every turtle. Even though computers can do things very quickly,
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Chapter 8 Arrays and Strings
Microsoft Visual C++.NET Chapter 61 Memory Management.
Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
CS107 References and Arrays By Chris Pable Spring 2009.
Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.
Built-in Data Structures in Python An Introduction.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
int [] scores = new int [10];
Multidimensional Arrays Computer and Programming.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
CSC 4630 Perl 2 adapted from R. E. Beck. I/O and Arithmetic Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the.
Chapter 9 Introduction to Arrays Fundamentals of Java.
M10 WS11:Krankenhausbedarfsplanung The World as We See It given by Gabriel Wurzer and Wolfgang E. Lorenz
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
String and Lists Dr. José M. Reyes Álamo.
MIS 643 Agent-Based Modeling NetLogo: Summary of Manual and Dictionary.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Perl Variables: Array Web Programming.
Creation, Traversal, Insertion and Removal
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Data Structures – 1D Lists
String and Lists Dr. José M. Reyes Álamo.
Fundaments of Game Design
Python List.
Introduction to Computer Science
String Objects & its Methods
Presentation transcript:

NETLOGO LISTS Or… James says, fput THIS!

What are LISTS? Shopping list Address list DNA 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 [ ] [ ] [ a sam ] [ [ -1 0 ] [ 1 1 ] [ 1 -1 ] ] [ g a c t ] [ 12 bob [ ] ]

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 [ ] [ ] [ Hello! I am James! ] BAD [ 1, 2, 3, 4 ] { 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 turtles 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 [ ] set dna [ c t a g a g t t ]

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

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-thing O> [ -2 5 ] Print list (xcor * 2) color T> [ ]

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> [ walrus ] print (list population) ; one item O> [ ] 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> [ ]

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 ]

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

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 ]

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

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.

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)

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 Reverse Reverses the order of the items in the list Sort Sorts the list Shuffle Randomizes the list

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

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 dont 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 [ ] 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> [ ] x 2 = [ ]

LISTS Code-Fu - MAP Double the value of each item in the list The easy way, with map set original-list [ ] set modified-list map [ ? * 2 ] original-list print original-list + x 2 = + modified-list O> [ ] x 2 = [ ]

LISTS Code-Fu - MAP Add two lists, making a third list set income [ ] set outgo [ ] set net-worth ( map [ ?1 - ?2 ] income outgo )

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

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!