06-morerec-post3up

# 06-morerec-post3up - More recursion Readings HtDP section...

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

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: More recursion Readings: HtDP, section 17. In this module: • a different definition of natural number; • a glimpse of non-structural recursion; • recursion on two items (lists or numbers). CS 135 Fall 2009 06: More recursion 1 Another definition of natural number This definition is more efficient for certain computations. A natural number is either • 0, or • 2 k , where k is a nonzero natural number , or • 2 k + 1 , where k is a natural number is a natural number because of the base case. 1 is a natural number because it is of the form 2(0) + 1 , and we have already shown to be a natural number. CS 135 Fall 2009 06: More recursion 2 2 is a natural number because it is of the form 2(1) . 3 is a natural number because it is of the form 2(1) + 1 . There are three clauses in the definition. The test for the first one is zero? and for the second one, even? . If n is of the form 2 k , then k is ( / n 2 ) . If n is of the form 2 k + 1 , then k is ( / ( sub1 n ) 2 ) . CS 135 Fall 2009 06: More recursion 3 ( define ( my-special-nat-fn n ) ( cond [( zero? n ) . . . ] [( even? n ) . . . ( my-special-nat-fn ( / n 2 )) . . . ] [ else . . . ( my-special-nat-fn ( / ( sub1 n ) 2 )) . . . ])) Note that if n is even, ( / n 2 ) is ( quotient n 2 ) . If n is odd, ( / ( sub1 n ) 2 ) is ( quotient n 2 ) . CS 135 Fall 2009 06: More recursion 4 The final template ( define ( my-special-nat-fn n ) ( cond [( zero? n ) . . . ] [( even? n ) . . . ( my-special-nat-fn ( quotient n 2 )) . . . ])) [ else . . . ( my-special-nat-fn ( quotient n 2 )) . . . ])) The advantage of this definition is that the depth of recursion is much smaller. If we start with 55 as the value of n , the recursion goes through 27 , 13 , 6 , 3 , 1 , . CS 135 Fall 2009 06: More recursion 5 What is this template good for? 55 = 2(27) + 1 55 = 2(2(13) + 1) + 1 = 2(2(13)) + 2 + 1 55 = 2(2(2(6) + 1)) + 2 + 1 = 2(2(2(6))) + 4 + 2 + 1 55 = 2(2(2(2(3) + 0))) + 4 + 2 + 1 55 = 2(2(2(2(2(1) + 1)))) + 4 + 2 + 1 55 = 2(2(2(2(2(2(0) + 1))))) + 16 + 4 + 2 + 1 55 = 32 + 16 + 4 + 2 + 1 CS 135 Fall 2009 06: More recursion 6 55 = 1 · 2 5 + 1 · 2 4 + 0 · 2 3 + 1 · 2 2 + 1 · 2 1 + 1 · 2 55 = 110111 2 , or 110111 in base 2. The base 2 representation of a number is also known as its binary representation. This data definition allows us to process the binary digits of a number, which is more efficient for certain computations. CS 135 Fall 2009 06: More recursion 7 A binary number is even if its last bit (binary digit) is 0 and odd if the last bit is 1. Computing the quotient when n is divided by 2 is the same as removing the last bit of n . For example, the binary representation of 55 is 110111 2 . Removing the last bit gives 11011 2 , which is 1 · 2 4 + 1 · 2 3 + 0 · 2 2 + 1 · 2 1 + 1 · 2 , or 27....
View Full Document

{[ snackBarMessage ]}

### Page1 / 25

06-morerec-post3up - More recursion Readings HtDP section...

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

View Full Document
Ask a homework question - tutors are online