Dan's Thoughts Thinking somewhat carefully

10Jun/090

Exercise 1.18 of SICP

Exercise 1.18: Using the results of exercises 1.16 and 1.17, devise a procedure that generates an iterative process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic number of steps.

(define (double x) (+ x x))
(define (halve x) (/ x 2))
(define (fast-mult-iter a b n)
  (cond ((= b 0) n)
        ((even? b) (fast-mult-iter (double a) (halve b) n))
        (else
          (fast-mult-iter a (- b 1) (+ a n)))))
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

Spam protection by WP Captcha-Free

No trackbacks yet.