{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

05-recursion-post3up

05-recursion-post3up - Working with recursion Readings HtDP...

This preview shows pages 1–5. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The list template: ( define ( my-list-fn lst ) ( cond [( empty? lst ) . . . ] [ else . . . ( first lst ) . . . ( my-list-fn ( rest lst )) . . . ])) The natural number template: ( define ( my-nat-fn n ) ( cond [( zero? n ) . . . ] [ else . . . ( my-nat-fn ( sub1 n )) . . . ])) CS 135 Fall 2009 05: Working with recursion 4 Example: a decreasing list Goal: countdown , which consumes a natural number n and produces a decreasing list of all natural numbers less than or equal to n . ( countdown 0 ) ( cons 0 empty ) ( countdown 2 ) ( cons 2 ( cons 1 ( cons 0 empty ))) 1 0 2 CS 135 Fall 2009 05: Working with recursion 5 We start filling in the template: ( define ( countdown n ) ( cond [( zero? n ) . . . ] [ else . . . ( countdown ( sub1 n )) . . . ])) If n is 0, we produce the list containing 0, and if n is nonzero, we cons n onto the countdown list for n 1 . CS 135 Fall 2009 05: Working with recursion 6
;; countdown: nat (listof nat) ;; produces a decreasing list of nats starting at n ;; example: ( check-expect ( countdown 2 ) ( cons 2 ( cons 1 ( cons 0 empty )))) ( define ( countdown n ) ( cond [( zero? n ) ( cons 0 empty )] [ else ( cons n ( countdown ( sub1 n )))])) CS 135 Fall 2009 05: Working with recursion 7 A condensed trace ( countdown 2 ) ( cons 2 ( countdown ( sub1 2 ))) ( cons 2 ( countdown 1 )) ( cons 2 ( cons 1 ( countdown ( sub1 1 )))) ( cons 2 ( cons 1 ( countdown 0 ))) ( cons 2 ( cons 1 ( cons 0 empty ))) CS 135 Fall 2009 05: Working with recursion 8 If the function countdown is applied to a negative argument, it will not terminate. The following variation is a little more robust. It can handle negative arguments more gracefully. ( define ( countdown n ) ( cond [( < = n 0 ) ( cons 0 empty )] [ else ( cons n ( countdown ( sub1 n )))])) CS 135 Fall 2009 05: Working with recursion 9

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
From definition to template A list is either empty or ( cons elt lst ) , where elt is an element and lst is a list. If my-list is ( cons elt lst ) , then elt is ( first my-list ) and lst is ( rest my-list ) . These expressions go into the template, with recursion on the second one. CS 135 Fall 2009 05: Working with recursion 10 ;; my-list-fn: (listof any) any ( define ( my-list-fn my-list ) ( cond [( empty? my-list ) . . . ] [ else ; my-list is of the form (cons elt lst) . . . ( first my-list ) ; this extracts elt ;; the next line applies recursion to lst . . . ( my-list-fn ( rest my-list )). . . ])) CS 135 Fall 2009 05: Working with recursion 11 The same process applied to the natural number definition yields the natural number template.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 20

05-recursion-post3up - Working with recursion Readings HtDP...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online