class 6: programming with data
DESCRIPTION
Procedure practice: middle, find-fixedpointHistory of SchemeIntroducing PairsTRANSCRIPT
![Page 1: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/1.jpg)
Class 6:Programming with Data
cs1120 Fall 2011David Evans5 September 2011
![Page 2: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/2.jpg)
Plan
Practice Procedures from Friday
Introducing Lists
![Page 3: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/3.jpg)
Practice Procedure: identity
1. Define a procedure, identity, that takes one input, and outputs that value.
![Page 4: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/4.jpg)
Practice Procedure: pick-oneDefine a procedure, pick-one, that takes three inputs. The first input is a Boolean value (either true or false). If the value of the first input is true, the output is the value of the second input; if the value of the first input is false, the output is the value of the third input.
![Page 5: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/5.jpg)
Practice Procedure: middle
Define a procedure, middle, that takes three numbers as inputs, and outputs the number that is in the middle.
a b c
![Page 6: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/6.jpg)
a < b
b < c
a < cb
c a
yes
yes no
yes no
b < c
a < c b
a c
yes no
yes no
![Page 7: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/7.jpg)
Binary Decision Diagram
Cartoon by Geoff Draper
![Page 8: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/8.jpg)
a < b
b < c
a < cb
c a
yes
yes no
yes no
b < c
a < c b
a c
yes no
yes no
(define middle (lambda (a b c)
![Page 9: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/9.jpg)
Is there a better middle procedure?
![Page 10: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/10.jpg)
A Better Middle?
(define middle (lambda (a b c) (min (max a b) (max b c) (max a c))))
Suggested by Peter Chapman
![Page 12: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/12.jpg)
Size matters…but isn’t everything: speed, clarity, memory use, etc.
![Page 13: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/13.jpg)
Practice Procedure: find-fixedpoint
Define a procedure, find-fixedpoint, that takes as input a function and an initial value, and outputs the fixed point of the function starting from that value. A fixed point of a function f is a value x such that (f x) evaluates to x.
![Page 14: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/14.jpg)
14
Ways to Design Programs
1. Think about what you want to do, and turn that into code.
2. Think about what you need to represent, and design your code around that.
Which is better?
![Page 15: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/15.jpg)
15
History of Scheme
Guy Steele
Guy Steele co-designed Scheme, with Gerry Sussman, and created the first Scheme interpreter for his 4th year project (1975)
More recently, Steele specified Java [1995]
![Page 16: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/16.jpg)
Pre-History of Scheme
Scheme [1975]Conniver [1973] Planner [1967]
Based on LISP [John McCarthy, 1958]
Based on Lambda Calculus [Alonzo Church, 1930s]
John McCarthy
![Page 17: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/17.jpg)
17
LISP
“Lots of Insipid Silly Parentheses”
“LISt Processing language”
Lists are really important – hard to write a useful Scheme program without them.
![Page 18: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/18.jpg)
18
Making Lists
Freedom Park, Tshwane/Pretoria, South Africa
![Page 19: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/19.jpg)
19
Making a Pair
> (cons 1 2)(1 . 2)
cons constructs a pair
1 2
![Page 20: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/20.jpg)
20
Splitting a Pair> (car (cons 1 2))1> (cdr (cons 1 2))2
car extracts first part of a paircdr extracts second part of a pair
1 2
car cdr
![Page 21: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/21.jpg)
21
Pair Examples> (cons (cons 1 2) 3))((1 . 2) 3)> (cdr (car (cons (cons 1 2) 3)))2> (car (car (cons 1 (cons 2 3))))car: expects argument of type <pair>; given 1
![Page 22: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/22.jpg)
22
Why “car” and “cdr”?• Original (1950s) LISP on IBM 704– Stored cons pairs in memory registers– car = “Contents of the Address part of the Register”– cdr = “Contents of the Decrement part of the Register”
(“could-er”)• Doesn’t matter unless you have an IBM 704
The names “car” and “cons” will come to have mnemonic significance only when we discuss the representation of the system in the computer.John McCarthy, Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I, 1960
![Page 23: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/23.jpg)
23
Implementing cons, car and cdr
Next class:how to define cons, car, and cdr ourselves!
![Page 24: Class 6: Programming with Data](https://reader033.vdocuments.mx/reader033/viewer/2022051208/546ce135af7959ea368b480c/html5/thumbnails/24.jpg)
24
Charge• Quiz Wednesday– Course book through Chapter 4– Gleick’s The Information, Chapters 1-3– Classes 1-5 (including all questions in the class
notes)
• Class Wednesday and Friday: Lots of examples and practice programming with procedures and recursive definitions