1 / 9

CS3L: Introduction to Symbolic Programming

CS3L: Introduction to Symbolic Programming. Summer 2008 Colleen Lewis colleenL@berkeley.edu. Lecture 9: Recursion Rocks!. Today. Copies (we need volunteers) Count-evens Find-evens Recursion In Two Directions. (copies 3 ‘ha). (copies 4 ‘ha). (copies 2 ‘ha). (se ‘ha. (se ‘ha.

galeno
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 9: Recursion Rocks!

  2. Today • Copies (we need volunteers) • Count-evens • Find-evens • Recursion In Two Directions

  3. (copies 3 ‘ha) (copies 4 ‘ha) (copies 2 ‘ha) (se ‘ha (se ‘ha (copies 1 ‘ha) (se ‘ha (copies 0 ‘ha) Copies (define (copies n wd) (if (< n 1) ‘() (sentence wd (copies (- n 1) wd)))) (se ‘ha ‘()

  4. Count the number of even-numbers (define (count-evens sent) (cond ((empty? sent) ;empty? 0 ) ;base case: return 0 ((even? (first sent)) (+ 1 (count-evens (bf sent))));recurse on the ; rest of sent ((odd? (first sent) (+ 0 (count-evens (bf sent))) ;recurse on the ; rest of sent ))

  5. The Leap of Faith… Dude this seems like a hard problem! I’ll do this small piece and hope that someone can do the rest.

  6. (se 2 sent = ( 3 4 5 6 ) sent = ( 4 5 6 ) (se 4 sent = ( 5 6 ) sent = ( 6 ) (se 6 sent = ( ) > (find-evens '(2 3 4 5 6)) sent = ( 2 3 4 5 6 ) ‘() • (se 2 (se 4 (se 6 ‘()))) • (2 4 6)

  7. Problem: find all the even numbers in a sentence of numbers (define (find-evens sent) (cond ((empty? sent) ;base case '() ) ((odd? (first sent)) ;rec case 1 (find-evens (bf sent)) ) (else ;rec case 2: even (se (first sent) (find-evens (bf sent))) ) ))

  8. The Leap of Faith… Dude this seems like a hard problem! I’ll do this small piece and hope that someone can do the rest. • Sum-in-interval • What was the “small piece”? • What was the “rest”?

  9. 5 10 4 9 Sum-In-Interval 4 10 4 + + 10

More Related