cs61a lecture 10 2011-07-06 colleen lewis. clicker poll where do you think youre learning the most?...
DESCRIPTION
TODAY Make a calculator program – To better understand how the Scheme interpreter works – STEP 1: calc-apply – STEP 2: list versus quote (Scheme primitives) – STEP 3: read (Scheme primitive) – STEP 4: read-print loop – STEP 5: read-eval-print loop – STEP 6: calc-evalTRANSCRIPT
![Page 1: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/1.jpg)
CS61A Lecture 10
2011-07-06Colleen Lewis
![Page 2: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/2.jpg)
Clicker poll Where do you think you’re learning the most?
I assume you’ll learn the most in lab & disc (so I won’t be offended)
A)LectureB)LabC)DiscussionD)Lab & DiscussionE)Lecture ≈ Lab ≈ Discussion
![Page 3: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/3.jpg)
TODAY
• Make a calculator program– To better understand how the Scheme interpreter
works– STEP 1: calc-apply– STEP 2: list versus quote (Scheme primitives)– STEP 3: read (Scheme primitive)– STEP 4: read-print loop– STEP 5: read-eval-print loop– STEP 6: calc-eval
![Page 4: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/4.jpg)
calc-apply
DEMO
![Page 5: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/5.jpg)
calc-apply
STk> (calc-apply '+ '(1 2 3))6STk> (calc-apply '* '(2 4 3))24STk> (calc-apply '/ '(10 2))5STk> (calc-apply '- '(9 2 3 1))3
![Page 6: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/6.jpg)
(define (calc-apply fn-wd arg-list) (cond ((equal? fn-wd '+)
(add-up-stuff-in arg-list)) ((equal? fn-wd '-)
(subtract-stuff-in arg-list)) ((equal? fn-wd '*)
(multiply-stuff-in arg-list)) ((equal? fn-wd '/)
(divide-stuff-in arg-list)) (else
(error "Calc: bad op: " fn-wd))))
![Page 7: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/7.jpg)
add-up-stuff-in(define (add-up-stuff-in lst) (accumulate + 0 lst))
STk> (accumulate + 0 '(1 2 4)).. -> + with args = (4 0).. <- + returns 4.. -> + with args = (2 4).. <- + returns 6.. -> + with args = (1 6).. <- + returns 77
![Page 8: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/8.jpg)
The two arguments to calc-apply are:
A) A word and a sentenceB) A word and a listC) A procedure and a sentenceD) A procedure and a listE) Not sure…
![Page 9: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/9.jpg)
list versus quote
![Page 10: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/10.jpg)
list versus quote
STk> '(1 2 +)(1 2 +)STk> (list 1 2 +)(1 2 #[closure arglist=args 7ff53de8])
![Page 11: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/11.jpg)
(read)
Demo
![Page 12: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/12.jpg)
Demo (read)
STk> (read)4545STk> (read)hellohelloSTk> (read)'hello(quote hello)
I typed this!
After I hit return, Scheme printed this
I didn’t have to quote
words' is really syntactic sugar for quote (a special form)
![Page 13: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/13.jpg)
Demo (read)
STk> (define a (read))helloaSTk> ahelloSTk>
![Page 14: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/14.jpg)
Demo (read)
STk> (define b (read))(+ 1 2)bSTk> b(+ 1 2)STk> (car b)+STk> (list-ref b 1)1
Not: #[closure arglist=args 7ff53de8]
![Page 15: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/15.jpg)
Demo (read)
STk> (define c (read))(+ 3 (+ 1 2))cSTk> (list-ref c 2)(+ 1 2)STk> (car c)+
Woah! read figured out it was a list within a list.
![Page 16: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/16.jpg)
Demo (read)
STk> (define d (read))(+ 3
)dSTk> d(+ 3)
read waits for me to put
necessary close-parens
![Page 17: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/17.jpg)
read Summary
• Prompts user for input• NOT a function• Whatever the user types it returns– They can type words (without quotes)– They can type numbers– They can type lists• If it looks like a list it waits for you to put necessary
close parentheses
![Page 18: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/18.jpg)
Read-Print Loop
![Page 19: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/19.jpg)
(read-print)
(define (read-print)
(display "type here: ")
(flush)
(print (read))
(read-print))
display prints stuff
Make the line above visible
Waits for user input
print prints stuff on a new
line
recursive call (infinite loop)
![Page 20: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/20.jpg)
STk> (read-print)type here: 44type here: hihitype here: (+ 1 2)(+ 1 2)type here: (+ (+ 3 4) 2)(+ (+ 3 4) 2)type here:
Infinite loop!
I’m typing HERE not at STk>
![Page 21: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/21.jpg)
Read-Eval-Print Loop
![Page 22: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/22.jpg)
(calc) demo
STk> (calc)calc: 11calc: (+ 2 3)5calc: (+ 2 (* 3 4))14
(read-print) Was sort of silly(calc)
actually does something
![Page 23: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/23.jpg)
(calc) demo – it doesn’t have variables or “real” functions
calc: +*** Error: Calc: bad expression: +Current eval stack:STk> (calc)calc: x*** Error: Calc: bad expression: xCurrent eval stack:
![Page 24: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/24.jpg)
(read-print)
(define (read-print)
(display "type here: ")
(flush)
(print (read))
(read-print))
![Page 25: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/25.jpg)
(calc) read-eval-print loop
(define (calc)
(display "calc: ")
(flush)
(print (calc-eval (read)))
(calc))
![Page 26: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/26.jpg)
Representing Math
+
Translating to Scheme(+ 1 2)car: + cdr: (1 2)
Children
![Page 27: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/27.jpg)
Representing Math in Scheme
(+ (* 2 4) 5)car: + cdr: ((* 2 4) 5)
![Page 28: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/28.jpg)
Representing Math in Scheme
(+ (* 3 (+ 2 1) 4) 5)car: + cdr:((* 3 (+ 2 1) 4) 5)
How many open parens?A) 1 B) 2 C) 3 D) 4 E) 5
![Page 29: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/29.jpg)
Remember the(calc) read-eval-print loop?
(define (calc)
(display "calc: ")
(flush)
(print (calc-eval (read)))
(calc))
![Page 30: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/30.jpg)
calc-eval base case
STk> (calc)calc: 11(define (calc-eval exp)
(cond ((number? exp) exp)
((list? exp) _____________ (else (error “Calc: bad exp”))))
![Page 31: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/31.jpg)
Remember calc-apply?
STk> (calc-apply '+ '(1 2 3))6STk> (calc-apply '* '(2 4 3))24STk> (calc-apply '/ '(10 2))5STk> (calc-apply '- '(9 2 3 1))3
![Page 32: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/32.jpg)
Remember map?
STk> (map square ‘(1 2 3))(1 4 9)
![Page 33: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/33.jpg)
calc-evalSTk> (calc)calc: (+ 1 2)3(define (calc-eval exp) (cond
((number? exp) exp) ((list? exp) (calc-apply
(car exp) (map calc-eval (cdr exp))))
(else (error “Calc: bad exp”))))
‘+‘(1 2)
+
![Page 34: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/34.jpg)
calc-evalSTk> (calc)calc: (+ (* 2 4) 5)40(define (calc-eval exp) (cond
((number? exp) exp) ((list? exp) (calc-apply
(car exp) (map calc-eval (cdr exp))))
(else (error “Calc: bad exp”))))
‘+‘(8 5)
![Page 35: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/35.jpg)
(calc-eval exp)Works for
bigger trees!
(calc-apply (car exp) (map calc-eval (cdr exp))))
![Page 36: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/36.jpg)
deep-map
![Page 37: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/37.jpg)
Remember map? Meet deep-mapSTk> (map square ‘(1 2 3))(1 4 9)STk> (deep-map square ‘(1 2 3))(1 4 9)STk> (deep-map square ‘((3 . 4) (5 6)))((9 . 16) (25 36))STk> (deep-map square 3)9STk> (deep-map square ‘())()
![Page 38: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/38.jpg)
deep-map base casesSTk> (deep-map square 3)9STk> (deep-map square ‘())()
(define (deep-map fn arg) (cond
((null? arg) '()) ((pair? arg) _____________ (else (fn arg))))
![Page 39: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/39.jpg)
Draw ‘((3 . 4) (5 6))
How many pairs?A) 1 B) 2 C) 3 D) 4 E) 5
![Page 40: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/40.jpg)
Draw ‘((3 . 4) (5 6))car cdr car cdr
3
car cdr
4
car cdr
6
car cdr
5
SOLUTION
![Page 41: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/41.jpg)
(deep-map sq ‘((3 . 4) (5 6))car cdr car cdr
3
car cdr
4
car cdr
6
car cdr
5
car cdr
?
?
(cons (deep-map fn (car arg)) (deep-map fn (cdr arg)))
![Page 42: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/42.jpg)
deep-map solution
(define (deep-map fn arg) (cond
((null? arg) '())((pair? arg) (cons (deep-map fn (car arg)) (deep-map fn (cdr arg))))(else (fn arg))))
![Page 43: CS61A Lecture 10 2011-07-06 Colleen Lewis. Clicker poll Where do you think youre learning the most? I assume youll learn the most in lab disc (so](https://reader036.vdocuments.mx/reader036/viewer/2022062413/5a4d1b757f8b9ab0599b6c47/html5/thumbnails/43.jpg)
map
(define (map fn seq)(if (null? seq)
‘()(cons (fn (car seq))
(map fn (cdr seq)))))