1.1 Monday Morning
Introductions.
What is HtDP? (Analogy to astronomy around 1500, or some better intro.)
Computation starts in first grade with 1 + 1 = 2. Then you learn rules like precedence. Then it gets interesting when you learn substitution in algebra.
Computation in Scheme uses slightly different notations – more consistent, it turns out, in the long run. (Use board then DrScheme)
Not just numbers: booleans, strings, images.
So much for computation. Now, to programming...
Design (recipe by example):
; current-age-minutes : number number -> number |
; To convert the difference in seconds to minutes, rounding down |
(check-expect (current-age-minutes 0 0) 0) |
(check-expect (current-age-minutes 0 60) 1) |
(check-expect (current-age-minutes 0 67) 1) |
(check-expect (current-age-minutes 0 789) 13) |
(define (current-age-minutes birth now) |
(quotient (- now birth) 60)) |
Show first design recipe.
Design with reuse:
; display-minutes : number number -> image |
; To convert the difference in seconds to a minute display |
(check-expect (display-minutes 0 0) (text "0" 12 "black")) |
(check-expect (display-minutes 0 789) (text "13" 12 "black")) |
(define (display-minutes birth now) |
(text (number->string (current-age-minutes birth now)) 12 "black")) |
Conditionals (leads to refined design recipe):
; display-age : number number -> image |
; To convert the difference in seconds to an age display, |
; in seconds (up to a minute) with "s", minutes (up to an hour) |
; with "m", or hours with "h" |
(check-expect (display-age 0 0) (text "0s" 12 "black")) |
(check-expect (display-age 0 789) (text "13m" 12 "black")) |
(check-expect (display-age 0 3607) (text "1h" 12 "black")) |
(define (display-age birth now) |
(cond |
[(zero? (current-age-minutes birth now)) |
(time->image (- now birth) "s")] |
[(> (current-age-minutes birth now) 60) |
(time->image (quotient (current-age-minutes birth now) 60) "h")] |
[else |
(time->image (current-age-minutes birth now) "m")])) |
; Via wish list: |
; time->image : number string -> image |
(check-expect (time->image 0 "s") (text "0s" 12 "black")) |
(check-expect (time->image 10 "h") (text "10h" 12 "black")) |
(define (time->image n s) |
(text (string-append (number->string n) s) 12 "black")) |