SICP 2.5 习题答案
程序员文章站
2022-07-14 10:57:13
...
通过数学的方法可以解决,假设乘积n不是偶数,那a肯定是0。
(define (power x n r) (if (< n 1) r (power x (- n 1) (* r x)))) (define (cons a b) (* (power 2 a 1) (power 3 b 1))) (define (car n) (define (car-iter x counter) (if (not(even? x)) counter (car-iter (/ x 2) (+ counter 1)))) (car-iter n 0)) (define (cdr n) (define (cdr-iter x counter) (cond ((= x 1) counter) ((even? x) (cdr-iter (/ x 2) counter)) (else (cdr-iter (/ x 3) (+ counter 1))))) (cdr-iter n 0))