Solution to SICP Exercise 1.17

Structure and Interpretation of Computer Programs

Solution to Exercise 1.17:

(define (double x)
(+ x x))

(define (halve x)
(/ x 2))

(define (even? n)
(= (remainder n 2) 0))

(define (fast-mult a b)
(cond ((or (= a 0) (= b 0)) 0)
((= b 1) a)
((even? b) (double (fast-mult a (halve b))))
(else (+ a (fast-mult a (- b 1))))))

Advertisements