Here are some routines I used to learn scheme with HtDP and SICP.
(define (Fahrenheit->Celsius F)
(* (- F 32) 5/9)
)
code
;;middle-of-3 : number number number -> number
;;to produces the number that would be in between
;;the other two if they were listed in increasing order
;;example:(middle-of-3 5 3 7) should be 5s
(define (middle-of-3 x y z)
(cond
[(< x y) ;for the case when x<y; then, if x<z, compare y & z; else, x must be the middle
(cond
[(< x z) ;for the case when x<z
(cond
[(< y z) y] ;if y<z, then the middle must be y
[else z])] ;else, the middle must be z
[else
x])];if x>=z, the middle must be x
[else ;for the case when x>=y; then, if x<z, x must be the meddle; else, compare y & z
(cond
[(< x z) ;if x<z, the middle must be x
x]
[else
(cond
[(< y z) z] ;if y<z, z must be the meddle
[else y])])])) ;if y>=z, y must be the meddle
code
;;interval-proper-subset? : number number number number -> boolean
;;to determine whether set of points in the first interval is a
;;subset of the set of points contained in the second interval,
;;but not equal to it.
;;example: (interval-proper-subset? 3 4 2 5) should be ture, but
;; (interval-proper-subset? 3 4 3 4) should be false
(define (interval-proper-subset? left1 right1 left2 right2)
(and
(and ;interval1 must be a subset of interval2
(>= left1 left2)
(<= right1 right2))
(not ;interval1 must not equal to interval2
(and
(= left1 left2)
(= right1 right2)))))
return the day in the year for a given date code
;;is-leap-year : number -> number
;;to determine whether the year is leap; if leap, then return 1; else return 0
(define (is-leap-year year)
(cond
[(= (remainder year 400) 0) ;when year is divisible by 400 the year is leap
1]
[(and ;when year is divisible by 4 but not the century year (-00) the year is leap
(= (remainder year 4) 0)
(not
(= (remainder year 100) 0)))
1]
[else ;else year is not leap year
0]))
;;day-within-year : symbol number number -> number
;;to calculate the day of the year that the date falls
;;example: (day-within-year 'Sep 24 2000) = 268
(define (day-within-year month day year)
(cond
[(symbol=? month 'Jan)
day]
[(symbol=? month 'Feb)
(+ 31 day)]
[(symbol=? month 'Mar)
(+
(+
(+ 31 28)
day)
(is-leap-year year))]
[(symbol=? month 'Apr)
(+
(+
(+ 31 28 31)
day)
(is-leap-year year))]
[(symbol=? month 'May)
(+
(+
(+ 31 28 31 30)
day)
(is-leap-year year))]
[(symbol=? month 'Jun)
(+
(+
(+ 31 28 31 30 31)
day)
(is-leap-year year))]
[(symbol=? month 'Jul)
(+
(+
(+ 31 28 31 30 31 30)
day)
(is-leap-year year))]
[(symbol=? month 'Aug)
(+
(+
(+ 31 28 31 30 31 30 31)
day)
(is-leap-year year))]
[(symbol=? month 'Sep)
(+
(+
(+ 31 28 31 30 31 30 31 31)
day)
(is-leap-year year))]
[(symbol=? month 'Oct)
(+
(+
(+ 31 28 31 30 31 30 31 31 30)
day)
(is-leap-year year))]
[(symbol=? month 'Nov)
(+
(+
(+ 31 28 31 30 31 30 31 31 30 31)
day)
(is-leap-year year))]
[(symbol=? month 'Dec)
(+
(+
(+ 31 28 31 30 31 30 31 31 30 31 30)
day)
(is-leap-year year))]))