diff options
author | rsiddharth <s@ricketyspace.net> | 2017-02-11 16:17:16 +0000 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2017-02-11 16:17:16 +0000 |
commit | 5077b12af5fae23d49aa6eea38ebc63c108df265 (patch) | |
tree | 5bb9aed1a857c051daca512897e6ccc4fd221a2c /net/ricketyspace | |
parent | b947a6167b51546a3deed1e99de33f10884e389e (diff) |
Add net/ricketyspace/sicp/one/seventeen.scm
Diffstat (limited to 'net/ricketyspace')
-rw-r--r-- | net/ricketyspace/sicp/one/seventeen.scm | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/one/seventeen.scm b/net/ricketyspace/sicp/one/seventeen.scm new file mode 100644 index 0000000..1a5ec15 --- /dev/null +++ b/net/ricketyspace/sicp/one/seventeen.scm @@ -0,0 +1,46 @@ + +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. + +;;; +;;; __________________ +;;; < +, - everything. > +;;; ------------------ +;;; \ . . . +;;; \ . . . ` , +;;; \ .; . : .' : : : . +;;; \ i..`: i` i.i.,i i . +;;; \ `,--.|i |i|ii|ii|i: +;;; U**U\.'@@@@@@`.||' +;;; \__/(@@@@@@@@@@)' +;;; (@@@@@@@@) +;;; `YY~~~~YY' +;;; || || +;;; + +(define-module (net ricketyspace sicp one seventeen) + #:export (fast-*)) + +(define (double a) + "Return A + A." + (+ a a)) + +(define (halve b) + "Return B/2. + +B must be an even number." + (cond ((= b 0) 0) + (else (+ 1 (halve (- b 2)))))) + +(define (even? b) + "Return #t if B is divisible by 2; #f otherwise." + (cond ((= b 1) #f) + ((= b 0) #t) + (else (even? (- b 2))))) + +(define (fast-* a b) + "Return A * B." + (cond ((= b 1) a) + ((even? b) (double (fast-* a (halve b)))) + (else (+ a (fast-* a (- b 1)))))) |