;; 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.3.1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor mixed-fraction #f #t none #f ())))
; Worked exercise 24.3.1

; nn-add : nat-num (m) nat-num (n) -> nat-num
(check-expect (nn-add 0 0) 0)
(check-expect (nn-add 0 1) 1)
(check-expect (nn-add 0 3) 3)
(check-expect (nn-add 1 0) 1)
(check-expect (nn-add 3 0) 3)
(check-expect (nn-add 3 8) 11)

(define (nn-add m n)
  (cond [(zero? n) m]
        [(positive? n)
         ; m                    natural              3
         ; n                    positive natural     8
         ; (sub1 n)             natural              7
         ; (nn-add m (sub1 n))  natural              10
         ; right answer         natural              11
         (add1 (nn-add m (sub1 n)))
         ]))