Skip to main content.
-->
June 27th, 2007

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))

Posted by Ken Dyck in Programming

2 Comments »

This entry was posted on Wednesday, June 27th, 2007 at 7:39 pm and is filed under Programming. You can follow any responses to this entry through the comments RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “Solution to SICP Exercise 2.5”

  1. Eli Bendersky says:

    Hello Ken,

    I began an endeavor similar to yours, reading SICP and posting my reading notes and solutions to all exercises. The work so far is here:
    http://eli.thegreenplace.net/category/programming/lisp/sicp/

    I enjoy reading your solutions and comparing with mine – keep them coming :-)

    Eli

  2. Ken Dyck says:

    That’s some great stuff, Eli. I’ll definitely be following along.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>