Solution to SICP Exercise 2.5

Structure and Interpretation of Computer Programs

Solution to Exercise 2.5:

(define (cons x y)
(* (expt 2 x)
(expt 3 y)))

(define (count-powers n d)
(define (iter i pow)
(if (zero? (remainder i d))
(iter (/ i d) (+ pow 1))
pow))
(iter n 0))

(define (car c)
(count-powers c 2))

(define (cdr c)
(count-powers c 3))

Advertisements