;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname 24.2.1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor mixed-fraction #f #t none #f ())))
; Worked exercise 24.2.1

; add-up-from-10 : natural>=10 -> number
(check-expect (add-up-from-10 8) 0)
(check-expect (add-up-from-10 10) 10)
(check-expect (add-up-from-10 11) 21)
(check-expect (add-up-from-10 15) 75) ; book says 65, which is wrong

(define (add-up-from-10 n)
  (cond [(< n 10) 0]
        [(= n 10) 10]         ; can merge this into the following case
        [(> n 10)
         ; n                  natural number > 10
         ; (- n 1)            natural number >= 10
         ; (add-up-from-10 (- n 1))   number
         (+ n (add-up-from-10 (- n 1)))
         ]))

; add-up-between : natural-number(m) natural-number(n) -> number
; Normally expects n >= m; otherwise returns 0.
(check-expect (add-up-between 8 6) 0)
(check-expect (add-up-between 8 8) 8)
(check-expect (add-up-between 7 9) 24)
(define (add-up-between m n)
  (cond [(< n m) 0]
        [(>= n m)    ; book says >, which is wrong
         ; n             natural number >= m
         ; (- n 1)       natural number >= m-1
         ; (add-up-between m (- n 1))   number
         (+ n (add-up-between m (- n 1)))
         ]))