• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Lesson 01 A Warmup Problem
 

Lesson 01 A Warmup Problem

on

  • 341 views

 

Statistics

Views

Total Views
341
Views on SlideShare
341
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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.

Lesson 01 A Warmup Problem Lesson 01 A Warmup Problem Presentation Transcript

  • 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
  • A simple job-interview problem
    Given a list of digits, compute its value as a decimal number.
  • Lists of Digits
    A Digit is one of
    "0" | "1" | "2" | ... | "9"
    A List of Digits (LOD) is one of:
    -- empty
    -- (cons Digit LOD)
  • Shortcut list notation
    (list 11 22 33)
    = (cons 11 (cons 22 (cons 33 empty)))
    (list 22) = (cons 22 empty)
    (list) = empty
  • 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!
  • Template for List functions
    ;; list-fn : LOX -> ??
    (define (list-fnlst)
    (cond
    [(empty? lst) ...]
    [else (... (first lst)
    (list-fn (rest lst)))]))
  • 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?
  • 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?
  • 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
  • Hmm, let's study the examples:
    (lod2number (list 4 2 3))
    = 4*100 + 2*10 + 3
    = 4*100 + (lod2number (list 2 3))
    = 4*10^2
    + (lod2number (list 2 3))
    = 4*(10^(length (list 2 3)))
    + (lod2number (list 2 3))
  • 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.
  • 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
  • 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]))
  • 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)
  • Hmm, what about power?
    We can use structural decomposition on NonNegInt:
    ;; A NonNegInt is one of
    ;; -- 0
    ;; -- (+ 1 NonNegInt)
  • Template for NonNegInt
    (define (f n)
    (cond
    [(zero? n) ...]
    [else (... (f (- n 1)))]))
  • power
    (define (power base n)
    (cond
    [(zero? n) 1]
    [else (* base (power base (- n 1)))]))
  • 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
  • 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)
  • 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.