exercise-17.scm (562B)
1 (define-library (sicp solutions chapter-1 exercise-17) 2 (import (scheme base)) 3 (export *-recursive 4 fast-*-recursive) 5 6 (begin 7 (define (*-recursive a b) 8 (if (= b 0) 9 0 10 (+ a (*-recursive a (- b 1))))) 11 12 (define (double n) 13 (* 2 n)) 14 15 (define (halve n) 16 (/ n 2)) 17 18 (define (fast-*-recursive a b) 19 (cond 20 ((= b 0) 0) 21 ((even? b) (+ (double a) 22 (fast-*-recursive a 23 (- b 2)))) 24 (else (+ a (fast-*-recursive a (- b 1))))))))