Trying out exercises from "Structure and Interpretation of Computer Programs" second edition
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
541 B

(define (divide a b)
(define (iter a b product)
(cond ((= a 0) product)
((< a 0) (- product 1))
(else (iter (- a b) b (+ product 1)))))
(iter a b 0))
; divide
(define (halve a)
(divide a 2))
(define (double a)
(+ a a))
(define (multiply a b)
(define (iter a b product)
(cond ((= a 1) (+ product b))
((even? a) (iter (halve a) (double b) product))
(else (iter (halve a) (double b) (+ product b)))))
(iter a b 0))
; multiply
(multiply 132 555)
(multiply 32 2)
(multiply 33 1)