Successfully reported this slideshow.
Upcoming SlideShare
×

# Lesson 01 A Warmup Problem

425 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Lesson 01 A Warmup Problem

1. 1. A Warmup Problem<br />CS 5010 Program Design Paradigms “Bootcamp”<br />Week 03, Lesson 1<br />TexPoint fonts used in EMF. <br />Read the TexPoint manual before you delete this box.: AAA<br />1<br />
2. 2. A simple job-interview problem<br />Given a list of digits, compute its value as a decimal number.<br />
3. 3. Lists of Digits<br />A Digit is one of<br /> "0" | "1" | "2" | ... | "9"<br />A List of Digits (LOD) is one of:<br />-- empty<br />-- (cons Digit LOD)<br />
4. 4. Shortcut list notation<br />(list 11 22 33) <br /> = (cons 11 (cons 22 (cons 33 empty)))<br />(list 22) = (cons 22 empty)<br />(list) = empty<br />
5. 5. lod->number<br />lod2number : LOD -> Number<br />Given a LOD, produce the number it represents<br />(lod2number (list 3)) = 3<br />(lod2number (list 2 1)) = 21<br />(lod2number (list 7 1 4)) = 714<br />(lod2number (list 7 14)) = ??<br />argument is not an LOD, so behavior is unspecified!<br />
6. 6. Template for List functions<br />;; list-fn : LOX -> ??<br />(define (list-fnlst)<br /> (cond<br /> [(empty? lst) ...]<br /> [else (... (first lst)<br /> (list-fn (rest lst)))]))<br />
7. 7. Template for List functions<br />;; list-fn : LOX -> ??<br />(define (list-fnlst)<br /> (cond<br /> [(empty? lst) ...]<br /> [else (... (first lst)<br /> (list-fn (rest lst)))]))<br />What's the answer for the empty list?<br />If we knew the answer for the rest of the list, and we knew the first of the list, how could we combine them to get the answer for the whole list?<br />
8. 8. lod2number<br />;; lod2number : LOD -> Number<br />(define (lod2numberlst)<br /> (cond<br /> [(empty? lst) ...]<br /> [else (... (first lst)<br /> (lod2number (rest lst)))]))<br />What's the answer for the empty list?<br />If we knew the answer for the rest of the list, and we knew the first of the list, how could we combine them to get the answer for the whole list?<br />
9. 9. Hmm, let's study the examples:<br />(lod2number (list 3)) = 3<br />(lod2number (list 2 3)) <br /> = 2*10 + 3<br /> = 2*10 + (lod2number (list 3))<br />(lod2number (list 4 2 3))<br /> = 4*100 + 2*10 + 3<br /> = 4*100 + (lod2number (list 2 3))<br />10^2<br />a list of length 2<br />
10. 10. Hmm, let's study the examples:<br />(lod2number (list 4 2 3))<br /> = 4*100 + 2*10 + 3<br /> = 4*100 + (lod2number (list 2 3))<br /> = 4*10^2 <br /> + (lod2number (list 2 3))<br /> = 4*(10^(length (list 2 3)))<br /> + (lod2number (list 2 3))<br />
11. 11. Conclusion<br />So, if lst is nonempty,<br />(lod2number lst) =<br /> (+ (* (digit2number (first lst))<br /> (power 10 (length (rest lst))))<br /> (lod2number (rest lst)))<br />What about empty?<br />(lod2number empty) = 0<br />(length empty) = 0, so everything works.<br />
12. 12. lod2number<br />;; lod2number : LOD -> Number<br />(define (lod2number lst)<br /> (cond<br /> [(empty? lst) 0]<br /> [else (+ (* (digit2number (first lst))<br /> (power 10 (length (rest lst))))<br /> (lod2number (rest lst))))]))<br />;; Add to wishlist:<br />digit2number : Digit -> Number<br />power : Number NonNegInt -> Number<br />Given base and exponent, produces base^exponent<br />
13. 13. Let's work on wishlist<br />;; digit2number : Digit -> Number<br />;; strategy: structural decomposition on Digit<br />(define (digit2number d)<br /> (cond<br /> [(string=? d "0") 0]<br /> [(string=? d "1") 1]<br /> [(string=? d "2") 2]<br /> [(string=? d "3") 3]<br /> [(string=? d "4") 4]<br /> [(string=? d "5") 5]<br /> [(string=? d "6") 6]<br /> [(string=? d "7") 7]<br /> [(string=? d "8") 8]<br /> [(string=? d "9") 9]))<br />
14. 14. Next we'll work on power<br />;; power : Number NonNegInt -> Number<br />;; Given base and exponent, produces base^exponent<br />(check-expect (power 10 0) 1)<br />(check-expect (power 10 1) 10)<br />(check-expect (power 10 2) 100)<br />(check-expect (power 10 3) 1000)<br />(check-expect (power 2 1) 2)<br />(check-expect (power 2 2) 4)<br />(check-expect (power 2 3) 8)<br />
15. 15. Hmm, what about power?<br />We can use structural decomposition on NonNegInt:<br />;; A NonNegInt is one of<br />;; -- 0<br />;; -- (+ 1 NonNegInt)<br />
16. 16. Template for NonNegInt<br />(define (f n)<br /> (cond<br /> [(zero? n) ...]<br /> [else (... (f (- n 1)))]))<br />
17. 17. power<br />(define (power base n)<br /> (cond<br /> [(zero? n) 1]<br /> [else (* base (power base (- n 1)))]))<br />
18. 18. Ready to test!<br />string=?: expects type <string> as 1st argument, given: 3; other arguments were: "0"<br />Oops– what happened?<br />I said: <br />A Digit is one of<br /> "0" | "1" | "2" | ... | "9"<br />But the tests were things like:<br />(check-expect (lod2number (list 3)) 3)<br />3 is not a digit.<br />"3" is a digit<br />
19. 19. Oops, our tests were wrong!<br />How did we determine that it was the fault of the tests?<br />Ans: we looked at the data definition!<br />Fix: add “..” ’s:<br />(check-expect (lod2number (list "3")) 3)<br />(check-expect (lod2number (list "2" "1")) 21)<br />(check-expect (lod2number (list "7" "1" "4")) 714)<br />
20. 20. Summary<br />Follow the recipe!<br />Use the template!<br />it tells you form of the program<br />it tells you what questions to ask for the blanks<br />Study the examples!<br />The wishlist helped us organize a program with several procedures.<br />