Sixteen lists and compound data. Recap Names: constants and variables When evaluated, return a specific data objects Can make new names with: [define.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often.
Two motion and change: programming with imperatives.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
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.
Fourteen lists and compound data. Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, , etc. “Floating-point” numbers ,
Loops – While, Do, For Repetition Statements Introduction to Arrays
Information Technology Center Hany Abdelwahab Computer Specialist.
Eight compound procedures and higher-order procedures.
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Thirteen conditional expressions: letting programs make “decisions”
Six compound procedures and higher-order procedures.
String Escape Sequences
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.
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 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Lists in Python.
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.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
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.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
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.
1 COMS 161 Introduction to Computing Title: The Digital Domain Date: September 6, 2004 Lecture Number: 6.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Artificial Intelligence Lecture No. 24 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Python Let’s get started!.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
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.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
int [] scores = new int [10];
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
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,
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
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.
CS314 – Section 5 Recitation 10
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Creation, Traversal, Insertion and Removal
The Metacircular Evaluator
T. Jumana Abu Shmais – AOU - Riyadh
CMSC 202 Java Primer 2.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
6.001 SICP Further Variations on a Scheme
Building Java Programs
Introduction to the Lab
Just Enough Java 17-May-19.
Presentation transcript:

sixteen lists and compound data

Recap Names: constants and variables When evaluated, return a specific data objects Can make new names with: [define name value] Applications (aka procedure calls) [procedure args …] Procedure is run with the args as inputs Conditionals (if expressions) [if test consequent alternative] Evaluate test If result was true, evaluate and return consequent, otherwise alternative Compound procedures [args … → exp] Makes a new procedure with the specified arguments and return value Expression for return value can refer to args

How compound procedures are called: An approximation [define double [n → [× n 2]] [double 4]  [[n → [× n 2]] 4] substitute value of double  [ [n → [× n 2] ] 4 ]  [× 4 2] replace call with body substitute argument into body  8 arg procedure body

Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, , etc. “Floating-point” numbers , -44.0, anything with a decimal point Characters Booleans (truth values) Two values: “true” and “false” Used to represent answers to questions 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 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 + 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 “[ ]” We’ll talk later about additional capabilities of map and fold

How map works (approximately) [map double [list ]]  [map [n → [× 2 n]] [list ]]  [list [[n → [× 2 n]] 1] [[n → [× 2 n]] 2] [[n → [× 2 n]] 3] [[n → [× 2 n]] 4] [[n → [× 2 n]] 5]]  [list [× 2 1] [[n → [× 2 n]] 2] [[n → [× 2 n]] 3] [[n → [× 2 n]] 4] [[n → [× 2 n]] 5]]  [list 2 [[n → [× 2 n]] 2] [[n → [× 2 n]] 3] [[n → [× 2 n]] 4] [[n → [× 2 n]] 5]]  [list 2 [× 2 2]] [[n → [× 2 n]] 3] [[n → [× 2 n]] 4] [[n → [× 2 n]] 5]]  [list 2 4 [[n → [× 2 n]] 3] [[n → [× 2 n]] 4] [[n → [× 2 n]] 5]]  …  [list [[n → [× 2 n]] 5]]  [list [× 2 5]]  [list ]  the list: [ ]

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 + 0 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] ►

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

Exercise 3 On web site Build a CD database using lists and strings Write procedures to extract information from it Doesn’t require understanding the rest of this lecture Due Tuesday

Esoteric issue: The textual representations of lists The rest of this lecture is esoteric You don’t need to fully 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