Fourteen lists and compound data. Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, -43589759, etc. “Floating-point” numbers 3.14159,

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Two motion and change: programming with imperatives.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables.
The Binary Numbering Systems
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.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Six compound procedures and higher-order procedures.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Eleven colors and rasters. Color objects [color name] Returns color with the specified name [color red green blue] Returns color with the specified amounts.
ECS15 for and sequence. Comments  Another way to repeat.
Sixteen lists and compound data. Recap Names: constants and variables When evaluated, return a specific data objects Can make new names with: [define.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Thirteen conditional expressions: letting programs make “decisions”
JavaScript, Third Edition
Six compound procedures and higher-order procedures.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Chapter 2 - Algorithms and Design
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
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.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Functional Programming in Scheme and Lisp.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Python Let’s get started!.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
CS314 – Section 5 Recitation 10
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Racket CSC270 Pepper major portions credited to
CS 115 Lecture 8 Structured Programming; for loops
While Loops in Python.
While Loops BIS1523 – Lecture 12.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Creation, Traversal, Insertion and Removal
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to Primitive Data types
String and Lists Dr. José M. Reyes Álamo.
For loops Taken from notes by Dr. Neil Moore
Topics Basic String Operations String Slicing
Introduction to the Lab
Topics Basic String Operations String Slicing
Introduction to Primitive Data types
Topics Basic String Operations String Slicing
Presentation transcript:

fourteen lists and compound data

Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, , etc. “Floating-point” numbers , -44.0, anything with a decimal point Booleans Procedures Note: there are good reasons for having different kinds of number types, but we’re not going to try to justify them at this point. Just be aware that they’re there.

Some composite data types Strings (i.e. text) Sequences of characters “this is a test”, “so is this” Colors Specify amount of red, green, and blue light for a pixel One integer (0-255) per “channel” (red, green, blue) One integer (0-255) for level of opacity (the “α-channel”) Bitmaps The kind of image manipulated by a paint program 2-dimensional array of color objects One color object per pixel of the image

A new composite data type: the list Lists are sequences of data objects Any length Any type of data object Different types of data can appear in the same list Example: a CD database A list of data objects representing CDs Each CD is represented as its own list: Artist Title Year

Okay, what can we do with lists? [list item 1 item 2 … item n ] Creates a list containing the specified items, in order [get list index] Extracts the item from list at position index (a number) Index counts from zero So for an 5-element list, the elements are numbered 0, 1, 2, 3, 4 [first list], [second list], …, [tenth list] Extracts the specified element of list. [length list] Returns the number of items in list [append list 1 list 2 … list n ] Creates a new list with all the items in the input lists, in order

Examples Notice that lists print in the same bracket notation as code This may seem confusing, but The system doesn’t print code back at you So you can’t mistake them for one another It will turn out to be really useful to have lists and code look the same ► [define my-list [list 1 2 3]] [1 2 3] ► [append my-list [list 4 5 6]] [ ] ► [get [append my-list [list 4 5 6]] 5] 6 ►

Mapping and folding [fold p list] or: [fold p start-value list] Joins all the values of list together using procedure p Mostly useful for summing, multiplying, or otherwise mushing together the elements of a list [fold + [list 1 2 3]] returns: [+ 3 [+ 2 1]] [fold + 0 [list 1 2 3]] returns: “[+ 3 [+ 2 [+ 1 0]]]” [map procedure list] Runs procedure on every element of list and returns their return values as a new list [map – [list ]] returns “[ ]”

Averaging Sum of a list fold + over all elements Mean (average) of list Divide by the length (number of elements) RMS (root-mean-square) A.k.a. standard deviation Easy: Take the root of the mean of the squares of all elements Get the squares by mapping square over all the elements ► [define sum [list → [fold + list]]] ► [define mean [list → [∕ [sum list] [length list]]]] ► [define rms [list → [sqrt [mean [map square list]]]]] ► [define square [number → [× number number]]] ► [rms [list ]]

Searching and filtering [list-index predicate list] Returns the position within list of the first item that satisfies predicate [find predicate list] Same, but returns the item itself [filter predicate list] Same, but returns all the items (as a list) satisfying predicate

Examples We’ll use this list several times, so put it in a variable Find all the numbers in it Find all the strings Find just the first number Find where the first number is Verify that position really has a number ► [define silly [list “a silly” “list” “with” 2 “numbers and” 5 “strings”]] ► [filter number? silly] [2 5] ► [filter string? silly] ["a silly" "list" "with" "numbers and" "strings"] ► [find number? silly] 2 ► [list-index number? silly] 3 ► [get silly 3] 2

Any and every item in a list [any predicate list] True when any item in list satisfies predicate [every predicate list] True when every item in list satisfies predicate ► [define cd-database [list [list “The white album” “The Beatles”] [list “ Sgt. Pepper's Lonely Hearts Club Band ” “The Beatles”] [list “Pod” “The Breeders”] [list “Dummy” “Portishead”]]] ► [define artist [album → [second album]]] ► [define Beatles? [album → [= [artist album] “The Beatles”]]] ► [any Beatles? cd-database] True ► [every Beatles? cd-database] False ► [filter Beatles? cd-database] [["The white album" "The Beatles"] ["Sgt. Pepper's Lonely Hearts Club Band" "The Beatles"]]

How do you count the number of Beatles albums in the database?

► [length [filter Beatles? cd-database]] 2 There’s also a built-in count procedure: [count Beatles? cd-database] By the way, we’re not holding you responsible for memorizing all these procedures For tests, we’ll provide you with the names and arguments for all the lists procedures you’ll need

How do you get the titles of all Beatles albums?

► [define album-title [album → [first album]]] ► [map album-title «procedure to apply to each element» [filter Beatles? cd-database]] «list of elements» ["The white album“ "Sgt. Pepper's Lonely Hearts Club Band"] ► We’ve slipped an extra feature into this: comments Anything enclosed in «»s is ignored by the computer. It’s just there as a note from the programmer

Extracting chunks of lists [prefix list count] The first count elements of list [without-prefix list count] All but the first count elements of list [suffix list count] [without-suffix list count] Elements taken from/dropped from the end rather than the beginning of the list ► [prefix [list ] 2] [1 2] ► [suffix [list ] 2] [3 4] ► [without-prefix [list ] 1] [2 3 4] ► [without-suffix [list ] 1] [1 2 3] ►

Iterating [up-to count proc] Runs proc repeatedly with inputs ranging from 0 to count (technically, count-1) Collects all the results as a list

Example How do you compute the sum of the squares of all the numbers from 1 to 99? [fold + [up-to 100 [n → [× n n]]] Or just: [fold + [up-to 100 square]] … if you’ve defined square already

Example How do you compute the sum of all the numbers from 1 to 99? [fold + [up-to 100 [n → n]]]

Last, but not least, the apply procedure [apply procedure list] Calls procedure and passes it the elements of list as arguments Really, really useful ► [apply + [list ]] 15 ► [apply max [list ]] 5 ► [apply get [list [list ] 2]] 3 ► [apply append [list [list 1 2] [list 3 4] [list 5 6]]] [ ]

The mystery revealed [define iterated-group [proc count → [apply group [up-to count proc]]]] First, up-to calls proc with inputs from 0 to count And returns their results as a single list Then, apply calls group with the elements of that list as arguments Finally, group makes them into a single picture

Esoteric issue: The textual representations of lists The rest of this lecture is esoteric You don’t need to understand it yet But It will teach you a notation that you may find more convenient It will let you understand what’s going on if the system prints something at you in this notation

The textual representations of lists How do you call a procedure on the list [1 [2 3]]? That is, the list with two elements: The number 1 The list [2 3] Several ways you could imagine notating it: [my-procedure [1 [2 3]]] [my-procedure “[1 [2 3]]”] [my-procedure [list 1 [list 2 3]]]

Textual representations of lists We’ve been stuck with: [list 1 [list 2 3]] Good points Unambiguous Bad points More typing Looks different from the printed representation Slower than providing a pre-made list How can we notate pre-made lists as arguments? Need another kind of quotation mark

The quote operator We use single-quotes to notate lists Just like quoting strings but Result is a list, not a string Use single-quote, not double-quote Only use it at the beginning: ‘[1 [2 3]] is the equivalent of [list 1 [list 2 3]]

More on quoting ‘[1 2 3]≈ [list 1 2 3] ‘[1 [2 3]]≈ [list 1 [list 2 3]] Do not put ‘ inside of ‘ as in: ‘[1 ‘[2 3]] ‘[1 “foo”]≈ [list 1 “foo”] ‘[1 foo]≠ ‘[1 “foo”] ‘[1 foo] isn’t illegal syntax And it isn’t a list with a string It’s a list with a new kind of data object: a symbol

Symbols Like strings but: They can only be individual words – no spaces, brackets, etc. When embedded in a quoted list, you don’t need any extra quote marks ‘[a b c] vs. ‘[“a” “b” “c”] Use eq? to check if two symbols are the same, not string=? Good because eq? happens to be a lot faster than string=? Faster and more convenient than strings Less flexible than strings

Ow, my head hurts [+ a 1] is a procedure call expression Returns the value of the variable a + 1 ‘[+ a 1] is not a procedure call expression It’s a quoted list: First element is the symbol + Second element is the symbol a Third element is the number 1 The + isn’t a reference to adding The a isn’t a reference to the variable a ‘a is the symbol a, not the variable a

Don’t panic Yes, quote is confusing But you’ll get used to it We’ll ease into using it It will turn out to be mind-bogglingly useful Just remember it isn’t really any different from the quotation marks in strings