あけましておめでとうございます。
インクリメント関数、デクリメント関数
(define inc (lambda (x) (+ x 1))) (define dec (lambda (x) (- x 1))) |
(define ++ inc) (define -- dec) (++ 1);;=> 2 (-- 1);;=> 0 |
(define plus (lambda (a b) (if (zero? a) b (inc (plus (dec a) b))))) |
;;recursive (plus 4 5) (inc (plus (dec 4) 5)) (inc (inc (plus (dec 3) 5))) (inc (inc (inc (plus (dec 2) 5)))) (inc (inc (inc (inc (plus (dec 1) 5))))) (inc (inc (inc (inc (plus 0 5)))) (inc (inc (inc (inc 5))) (inc (inc (inc 6))) (inc (inc 7)) (inc 8) 9 |
(define plus2 (lambda (a b) (if (zero? a) b (plus2 (dec a)(inc b))))) |
;;iterative (plus2 4 5) (plus2 (dec 4)(inc 5)) (plus2 (dec 3)(inc 6)) (plus2 (dec 2)(inc 7)) (plus2 (dec 1)(inc 8)) 9 |
0 件のコメント:
コメントを投稿