1 / 9

CS3L: Introduction to Symbolic Programming

CS3L: Introduction to Symbolic Programming. Summer 2008 Colleen Lewis colleenL@berkeley.edu. Lecture 11: Accumulating Recursion. Today. Upcoming Homework Thurs: Bowling (Hwk11) due Monday Fri: Compress/occurs-in? (Hwk12) due Tuesday Mon: Mini-project 2 due Wednesday

waylon
Download Presentation

CS3L: Introduction to Symbolic Programming

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. CS3L: Introduction to Symbolic Programming Summer 2008 Colleen Lewis colleenL@berkeley.edu Lecture 11: Accumulating Recursion

  2. Today • Upcoming Homework • Thurs: Bowling (Hwk11) due Monday • Fri: Compress/occurs-in? (Hwk12) due Tuesday • Mon: Mini-project 2 due Wednesday • Lunch yesterday was fun ~10 people came • Accumulating Recursion • Recursion Patterns

  3. (sent-sum ‘(2 3 5 )) (sent-sum ‘(3 5)) (+ 2 (+ 3 (sent-sum ‘(5)) (+ 5 (sent-sum ‘()) Not Accumulating Recursion (define (sent-sum sent) (if (empty? sent) 0 (+ (first sent) (sent-sum (bf sent))))) >(sent-sum ‘(2 3 5)) 0 (+ 2 (+ 3 (+ 5 0)))

  4. (sent-sum ‘(2 3 5 ) 0) (sent-sum ‘(3 5) 2) (sent-sum ‘(5) 5) (sent-sum ‘() 10) Accumulating Recursion (define (sent-sum sent sum-so-far) (if (empty? sent) sum-so-far (sent-sum (bf sent) (+ sum-so-far (first sent))))) > (sent-sum ‘(2 3 5) 0)

  5. Recursion Patterns • Split into groups of 4 • I’ll pass out a set of worksheets for each group • Work on the work sheet as a team • Join another group and explain your worksheet to them

  6. Application-To-All (define (proc-applied-to-all sent) (if (empty? sent) ‘() (se (proc (first sent)) (proc-applied-to-all (bf sent))))))

  7. Examples of Application to All • Square-all • Add-1-to-all • Add-2-to-all • Grocery-to-cost (food->cost) • Calorie-counter (food -> calories) • French-to-english • English-to-piglatin

  8. Filtering (define (filtered sent) (cond ((empty? sent) ‘()) ((interesting? (first sent)) (sent (first sent) (filtered (bf sent)))) (else (filtered (bf sent)))))

  9. Examples of Filtering • Keep-Multiples-of-5 • Keep-Evens • Keep-vowels • Keep-words-with-even-num-letters • Keep-Michael • Gather-with-hair-color

More Related