Module 5

Module 5 - Working with recursion Readings: HtDP, sections...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Working with recursion Readings: HtDP, sections 11, 12, 13 (Intermezzo 2), 17. We can extend the idea of a self-referential definition to defining the natural numbers, which leads to the use of recursion in order to write functions that consume numbers. CS 135 Fall 2008 05: Working with recursion 1 Natural numbers A natural number is either 0, or it is 1 plus another (smaller) natural number. The analogy to the self-referential definition of lists can be made clearer by defining a “1 plus” function: ( define ( add1 n ) ( + 1 n )) ( add1 ) ⇒ 1 ( add1 ( add1 )) ⇒ 2 ( add1 ( add1 ( add1 ))) ⇒ 3 CS 135 Fall 2008 05: Working with recursion 2 We can use the similarity to lists to modify our design recipe. • add1 plays the role of cons • 0 plays the role of empty and zero? plays the role of empty? • If we ( define ( sub1 n ) (- n 1 )) then sub1 can play the role of rest • What plays the role of first ? CS 135 Fall 2008 05: Working with recursion 3 Adapting the list template ;; 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 2008 05: Working with recursion 4 Example: a decreasing list We will define the function countdown which consumes a natural number and produces a decreasing list of all natural numbers less than or equal to it. ( countdown ) should produce ( cons empty ) and ( countdown 2 ) should produce ( cons 2 ( cons 1 ( cons empty ))) 1 2 CS 135 Fall 2008 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 2008 05: Working with recursion 6 ;; countdown: nat → (listof nat) ;; produces a decreasing list of nats starting at n ;; example: (countdown 2) ⇒ (cons 2 (cons 1 (cons 0 empty))) ( define ( countdown n ) ( cond [( zero? n ) ( cons empty )] [ else ( cons n ( countdown ( sub1 n )))])) CS 135 Fall 2008 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 ))) ⇒ ( cons 2 ( cons 1 ( cons empty ))) CS 135 Fall 2008 05: Working with recursion 8 If the function countdown is applied to a negative argument, it will not terminate. Though a negative argument is a violation of the contract, the following variation is a little more robust and can handle negative arguments more gracefully....
View Full Document

This note was uploaded on 10/21/2010 for the course CS 135 taught by Professor Vasiga during the Fall '07 term at Waterloo.

Page1 / 40

Module 5 - Working with recursion Readings: HtDP, sections...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online