some solutions for MIT Press's "structure and interpretation of computer programs", I guess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB

;; setup
(define (inc a)
(+ a 1))
(define (dec a)
(- a 1))
;; my-plus 1
(define (my-plus a b)
(if (= a 0) b (inc (my-plus (dec a) b))))
(my-plus 4 5)
;; -> (my-plus 4 5)
;; -> (inc (my-plus (dec 4) 5))
;; -> (inc (my-plus 3 5))
;; -> (inc (inc (my-plus (dec 3) 5)))
;; -> (inc (inc (my-plus 2 5)))
;; -> (inc (inc (inc (my-plus (dec 2) 5))))
;; -> (inc (inc (inc (my-plus 1 5))))
;; -> (inc (inc (inc (inc (my-plus (dec 1) 5)))))
;; -> (inc (inc (inc (inc (my-plus 0 5)))))
;; -> (inc (inc (inc (inc 5))))
;; -> (inc (inc (inc 6)))
;; -> (inc (inc 7))
;; -> (inc 8)
;; => 9
;; a: recursive process
;; my-plus 2
(define (my-plus-again)
(if (= a 0) b (my-plus-again (dec a) (inc b))))
(my-plus-again 4 5)
;; -> (my-plus-again 4 5)
;; -> (my-plus-again (dec 4) (inc 5))
;; -> (my-plus-again 3 6)
;; -> (my-plus-again (dec 3) (inc 6))
;; -> (my-plus-again 2 7)
;; -> (my-plus-again (dec 2) (inc 7))
;; -> (my-plus-again 1 8)
;; -> (my-plus-again (dec 1) (inc 8))
;; -> (my-plus-again 0 9)
;; => 9
;; a: iterative process