与做
ab
的方法类似,将
b
减半,将a 加倍,直到
b
为0
(define (double x) (+ x x))
(define (halve x) (/ x 2))
(define (even? x)
(= (remainder x 2) 0))
(define (fast-multiply ret a b)
(cond ((= b 0) ret)
((even? b) (fast-multiply ret (double a) (halve b)))
(else (fast-multiply (+ ret a) a (- b 1)))))
(define (multiply a b)
(fast-multiply 0 a b))
(multiply 8 11)