1 / 43

CS61A Lecture 10

CS61A Lecture 10. 2011-07-06 Colleen Lewis. 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) Lecture Lab Discussion Lab & Discussion Lecture ≈ Lab ≈ Discussion. TODAY. Make a calculator program

leyna
Download Presentation

CS61A Lecture 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS61A Lecture 10 2011-07-06 Colleen Lewis

  2. 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) Lecture Lab Discussion Lab & Discussion Lecture ≈ Lab ≈ Discussion

  3. 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

  4. calc-apply DEMO

  5. calc-apply STk> (calc-apply '+ '(1 2 3)) 6 STk> (calc-apply '* '(2 4 3)) 24 STk> (calc-apply '/ '(10 2)) 5 STk> (calc-apply '- '(9 2 3 1)) 3

  6. (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))))

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

  8. The two arguments to calc-apply are: A word and a sentence A word and a list A procedure and a sentence A procedure and a list Not sure…

  9. list versus quote

  10. list versus quote STk> '(1 2 +) (1 2 +) STk> (list 1 2 +) (1 2 #[closure arglist=args 7ff53de8])

  11. (read) Demo

  12. Demo (read) 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) STk> (read) 45 45 STk> (read) hello hello STk> (read) 'hello (quote hello)

  13. Demo (read) STk> (define a (read)) hello a STk> a hello STk>

  14. Demo (read) Not: #[closure arglist=args 7ff53de8] STk> (define b (read)) (+ 1 2) b STk> b (+ 1 2) STk> (car b) + STk> (list-ref b 1) 1

  15. Demo (read) Woah! read figured out it was a list within a list. STk> (define c (read)) (+ 3 (+ 1 2)) c STk> (list-ref c 2) (+ 1 2) STk> (car c) +

  16. Demo (read) read waits for me to put necessary close-parens STk> (define d (read)) (+ 3 ) d STk> d (+ 3)

  17. 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

  18. Read-Print Loop

  19. (read-print) display prints stuff Make the line above visible print prints stuff on a new line Waits for user input recursive call (infinite loop) (define (read-print) (display "type here: ") (flush) (print (read)) (read-print))

  20. I’m typing HERE not at STk> Infinite loop! STk> (read-print) type here: 4 4 type here: hi hi type here: (+ 1 2) (+ 1 2) type here: (+ (+ 3 4) 2) (+ (+ 3 4) 2) type here:

  21. Read-Eval-Print Loop

  22. (calc) demo (read-print) Was sort of silly (calc) actually does something STk> (calc) calc: 1 1 calc: (+ 2 3) 5 calc: (+ 2 (* 3 4)) 14

  23. (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: x Current eval stack:

  24. (read-print) (define (read-print) (display "type here: ") (flush) (print (read)) (read-print))

  25. (calc) read-eval-print loop (define (calc) (display "calc: ") (flush) (print (calc-eval (read))) (calc))

  26. Representing Math 2 1 2 + 1 Children Translating to Scheme (+ 1 2) car: + cdr: (1 2)

  27. Representing Math in Scheme 5 4 + * 2 (+ (* 2 4) 5) car: + cdr: ((* 2 4) 5)

  28. Representing Math in Scheme How many open parens? A) 1 B) 2 C) 3 D) 4 E) 5 5 4 1 + * + 2 3 (+ (* 3 (+ 2 1) 4) 5) car: + cdr:((* 3 (+ 2 1) 4) 5)

  29. Remember the(calc)read-eval-print loop? (define (calc) (display "calc: ") (flush) (print (calc-eval (read))) (calc))

  30. calc-evalbase case STk> (calc) calc: 1 1 (define (calc-eval exp) (cond ((number? exp) exp) ((list? exp) _____________ (else (error “Calc: bad exp”))))

  31. Remembercalc-apply? STk> (calc-apply '+ '(1 2 3)) 6 STk> (calc-apply '* '(2 4 3)) 24 STk> (calc-apply '/ '(10 2)) 5 STk> (calc-apply '- '(9 2 3 1)) 3

  32. Remember map? STk> (map square ‘(1 2 3)) (1 4 9)

  33. calc-eval 2 + 1 ‘+ ‘(1 2) STk> (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”))))

  34. calc-eval 5 4 + * 2 ‘+ ‘(8 5) STk> (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”))))

  35. (calc-eval exp)Works for bigger trees! 4 + 5 2 * 2 4 + * 2 1 3 + * + 3 3 (calc-apply (car exp) (map calc-eval (cdr exp))))

  36. deep-map

  37. Remember map? Meet deep-map STk> (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) 9 STk> (deep-map square ‘()) ()

  38. deep-map base cases STk> (deep-map square 3) 9 STk> (deep-map square ‘()) () (define (deep-map fn arg) (cond ((null? arg) '()) ((pair? arg) _____________ (else (fn arg))))

  39. Draw ‘((3 . 4) (5 6)) How many pairs? A) 1 B) 2 C) 3 D) 4 E) 5

  40. Draw ‘((3 . 4) (5 6)) car car car car car cdr cdr cdr cdr cdr 4 5 3 6 SOLUTION

  41. (deep-map sq ‘((3 . 4) (5 6)) car car car car car car cdr cdr cdr cdr cdr cdr 4 5 3 6 (cons (deep-map fn (car arg)) (deep-map fn (cdr arg))) ? ?

  42. 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))))

  43. map (define (map fn seq) (if (null? seq) ‘() (cons (fn (car seq)) (map fn (cdr seq)))))

More Related