問題1.9 をやってみる

(define (inc x)
  (+ x 1))

(define (dec x)
  (- x 1))
  

;-----
;再帰的プロセス
(define (plus-1-9a a b)
  (if (= a 0)
      b
      (inc (plus-1-9a (dec a) b))))
      
;-----

(plus-1-9 4 5)
(inc (plus-1-9a (dec 4) 5))
(inc (plus-1-9a (- 4 1) 5))
(inc (plus-1-9a 3 5))


(inc (plus-1-9a 3 5))
(inc (inc (plus-1-9a (dec 3) 5)))
(inc (inc (plus-1-9a (- 3 1) 5)))
(inc (inc (plus-1-9a 2 5)))

(inc (inc (inc (plus-1-9a (dec 2) 5))))
(inc (inc (inc (plus-1-9a (- 2 1) 5))))
(inc (inc (inc (plus-1-9a 1 5))))

(inc (inc (inc (inc (plus-1-9a (dec 1) 5)))))
(inc (inc (inc (inc (plus-1-9a (- 1 1) 5)))))
(inc (inc (inc (inc (plus-1-9a 0 5)))))
(inc (inc (inc (inc 5)))))
(+ (+ (+ (+ 5 1) 1) 1) 1)
9

;-----
;反復的プロセス
(define (plus-1-9b a b)
  (if (= a 0)
      b
      (plus-1-9b (dec a) (inc b))))
      
;-----
(plus-1-9b 4 5)
(plus-1-9b (dec 4) (inc 5))
(plus-1-9b (- 4 1) (+ 5 1))
(plus-1-9b 3 6)
(plus-1-9b (dec 3) (inc 6))
(plus-1-9b (- 3 1) (+ 6 1))
(plus-1-9b 2 7)
(plus-1-9b (dec 2) (inc 7))
(plus-1-9b (- 2 1) (+ 7 1))
(plus-1-9b 1 8)
(plus-1-9b (dec 1) (inc 8))
(plus-1-9b (- 1 1) (+ 8 1))
(plus-1-9b 0 9)
9

ばらしてみるとよくわかる。
でも、パッと見でどうなるのかわかりにくいなぁ