This commit is contained in:
NaiJi ✨ 2020-08-21 20:22:56 +03:00
parent 9de52ba145
commit 75bce812b1
1 changed files with 28 additions and 0 deletions

28
sicp-ex-1.18.rkt Normal file
View File

@ -0,0 +1,28 @@
(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)