diff options
author | rsiddharth <s@ricketyspace.net> | 2017-11-04 22:49:27 +0000 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2017-11-04 22:49:27 +0000 |
commit | cc11528c51c86b9d64a14f9dd038b65444960e5f (patch) | |
tree | 06dce7f725867b0dc712fcb08ba992bf1e6333b9 /net/ricketyspace | |
parent | 8cbbb070f630e0eb23b152ffb4e318a2fdba02d0 (diff) |
net: Add (net ricketyspace sicp one thirtythree).
* net/ricketyspace/sicp/one/thirtythree.scm: New file.
Diffstat (limited to 'net/ricketyspace')
-rw-r--r-- | net/ricketyspace/sicp/one/thirtythree.scm | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/one/thirtythree.scm b/net/ricketyspace/sicp/one/thirtythree.scm new file mode 100644 index 0000000..6f54c35 --- /dev/null +++ b/net/ricketyspace/sicp/one/thirtythree.scm @@ -0,0 +1,68 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. + +;;; ___________________ +;;; < fuck abstraction. > +;;; ------------------- +;;; \ __ +;;; \ (oo) +;;; \ ( ) +;;; \ /--\ +;;; __ / \ \ +;;; UooU\.'@@@@@@`.\ ) +;;; \__/(@@@@@@@@@@) / +;;; (@@@@@@@@)(( +;;; `YY~~~~YY' \\ +;;; || || >> + +(define-module (net ricketyspace sicp one thirtythree) + #:use-module (net ricketyspace sicp one twentyeight) + #:export (filtered-accumulate sum-odd sum-prime-sqrs)) + +(define (filtered-accumulate combiner null-value term a next b filter) + (cond ((> a b) null-value) + ((not (filter a)) + (combiner null-value + (filtered-accumulate combiner null-value + term (next a) next b + filter))) + (else + (combiner (term a) + (filtered-accumulate combiner null-value + term (next a) next b + filter))))) + +(define (sum-odd n) + (let ((null-value 0) + (term (lambda (x) x)) + (next (lambda (x) (1+ x)))) + (filtered-accumulate + null-value term 0 next n odd?))) + +(define (sum-prime-sqrs a b) + "Return sum of prime squares between A and B. + +A and B must be an integer greater than zero." + (let ((null-value 0) + (term (lambda (x) (* x x))) + (next (lambda (x) (1+ x)))) + (filtered-accumulate + null-value term a next b prime?))) + + +;;; Guile REPL +;;; scheme@(guile-user)> ,use (net ricketyspace sicp one thirtythree) +;;; scheme@(guile-user)> (sum-odd 1) +;;; $9 = 1 +;;; scheme@(guile-user)> (sum-odd 5) +;;; $10 = 9 +;;; scheme@(guile-user)> (sum-odd 10) +;;; $13 = 25 +;;; scheme@(guile-user)> (sum-prime-sqrs 1 5) +;;; $10 = 39 +;;; scheme@(guile-user)> (+ 1 4 9 25) +;;; $11 = 39 +;;; scheme@(guile-user)> (sum-prime-sqrs 1 10) +;;; $12 = 88 +;;; scheme@(guile-user)> (+ 1 4 9 25 49) +;;; $13 = 88 + |