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