Skip to content

Instantly share code, notes, and snippets.

@tetsu-miyagawa
Created July 2, 2015 22:19
Show Gist options
  • Save tetsu-miyagawa/0cb848548424ad7b8322 to your computer and use it in GitHub Desktop.
Save tetsu-miyagawa/0cb848548424ad7b8322 to your computer and use it in GitHub Desktop.
SICP Exercise 1.18
(define (fast-multi a b)
(define (double n) (+ n n))
(define (halve n) (/ n 2))
(define (even? n) (= (remainder n 2) 0))
(define (iter a b x)
(cond ((or (= a 0) (= b 0)) 0)
((= b 1) (+ a x))
((even? b) (iter (double a) (halve b) x))
(else (iter a (- b 1) (+ a x)))))
(iter a b 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment