1.18|

$ cd ~

SICP Exercise 1.18

Initially I solved it the same way as fast-exp. Just remember that an iterative process can be completely defined, at any point, by its state variables.

(define (mul-iter a b)
  (define (iter counter a b)
    (cond ((= b 0) counter)
          ((even? b) (iter counter (double a) (halve b)))
          (else (iter (+ counter a) a (- b 1)))))
  (iter 0 a b))

(define (double x) (+ x x))
(define (halve x) (/ x 2))