CS61A Lecture 10Clicker poll TODAYcalc-applySlide 5PowerPoint Presentationadd-up-stuff-inThe two arguments to calc-apply are:list versus quoteSlide 10(read)Demo (read)Slide 13Slide 14Slide 15Slide 16read SummaryRead-Print Loop(read-print)Slide 20Read-Eval-Print Loop(calc) demo(calc) demo – it doesn’t have variables or “real” functionsSlide 24(calc) read-eval-print loopRepresenting MathRepresenting Math in SchemeSlide 28Remember the(calc) read-eval-print loop?calc-eval base caseRemember calc-apply?Remember map?calc-evalSlide 34(calc-eval exp) Works for bigger trees!deep-mapRemember map? Meet deep-mapdeep-map base casesDraw ‘((3 . 4) (5 6))Slide 40(deep-map sq ‘((3 . 4) (5 6))deep-map solutionmapCS61A Lecture 102011-07-06Colleen LewisClicker 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 ≈ DiscussionTODAY•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-evalcalc-applyDEMOcalc-applySTk> (calc-apply '+ '(1 2 3))6STk> (calc-apply '* '(2 4 3))24STk> (calc-apply '/ '(10 2))5STk> (calc-apply '- '(9 2 3 1))3(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))))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 77The 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…list versus quotelist versus quoteSTk> '(1 2 +)(1 2 +)STk> (list 1 2 +)(1 2 #[closure arglist=args 7ff53de8])(read)DemoDemo (read)STk> (read)4545STk> (read)hellohelloSTk> (read)'hello(quote hello)I typed this!After I hit return, Scheme printed thisI didn’t have to quote words' is really syntactic sugar for quote (a special form)Demo (read)STk> (define a (read))helloaSTk> ahelloSTk>Demo (read)STk> (define b (read))(+ 1 2)bSTk> b(+ 1 2)STk> (car b)+STk> (list-ref b 1)1Not: #[closure arglist=args 7ff53de8]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.Demo (read)STk> (define d (read))(+ 3 )dSTk> d(+ 3)read waits for me to put necessary close-parensread 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 parenthesesRead-Print Loop(read-print)(define (read-print) (display "type here: ") (flush) (print (read)) (read-print))display prints stuffMake the line above visibleWaits for user inputprint prints stuff on a new linerecursive call (infinite loop)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>Read-Eval-Print Loop(calc) demoSTk> (calc)calc: 11calc: (+ 2 3)5calc: (+ 2 (* 3 4))14 (read-print) Was sort of silly(calc) actually does something(calc) demo – it doesn’t have variables or “real” functionscalc: +*** Error: Calc: bad expression: +Current eval stack:STk> (calc)calc: x*** Error: Calc: bad expression: xCurrent eval stack:(read-print)(define (read-print) (display "type here: ") (flush) (print (read)) (read-print))(calc) read-eval-print loop(define (calc) (display "calc: ") (flush) (print (calc-eval (read))) (calc))Representing Math+Translating to Scheme(+ 1 2)car: + cdr: (1 2)ChildrenRepresenting Math in Scheme(+ (* 2 4) 5)car: + cdr: ((* 2 4) 5)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) 5Remember the(calc) read-eval-print loop?(define (calc) (display "calc: ") (flush) (print (calc-eval (read))) (calc))calc-eval base caseSTk> (calc)calc: 11(define (calc-eval exp)(cond ((number? exp) exp) ((list? exp) _____________ (else (error “Calc: bad exp”))))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))3Remember map?STk> (map square ‘(1 2 3))(1 4 9)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)+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)(calc-eval exp)Works for bigger trees!(calc-apply (car exp) (map calc-eval (cdr exp))))deep-mapRemember 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 ‘())()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))))Draw ‘((3 . 4) (5 6))How many pairs?A) 1 B) 2 C) 3 D) 4 E) 5Draw ‘((3 . 4) (5 6))car cdrcar cdr3car cdr4car cdr6car cdr5SOLUTION(deep-map sq ‘((3 . 4) (5 6))car cdrcar cdr3car cdr4car cdr6car cdr5car cdr??(cons (deep-map fn (car arg)) (deep-map fn (cdr arg)))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))))map(define (map fn seq)(if (null? seq)‘()(cons (fn (car seq))(map fn (cdr
View Full Document