sicp

sicp sandbox.
git clone git://git.ricketyspace.net/sicp.git
Log | Files | Refs

commit 1b3b4fa04de06786427cf3753962236d28a40ecd
parent 71b1de4198dad80ffc8755c34aa48a39ef512226
Author: rsiddharth <s@ricketyspace.net>
Date:   Fri, 20 Oct 2017 02:05:46 +0000

net: Add (net ricketyspace sicp one thirtytwo)

* net/ricketyspace/sicp/one/thirtytwo.scm: New file.

Diffstat:
net/ricketyspace/sicp/one/thirtytwo.scm | 36++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+), 0 deletions(-)

diff --git a/net/ricketyspace/sicp/one/thirtytwo.scm b/net/ricketyspace/sicp/one/thirtytwo.scm @@ -0,0 +1,36 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. + +(define-module (net ricketyspace sicp one thirtytwo) + #:export (factorial simpson)) + +(define (accumulate combiner null-value term a next b) + (if (> a b) + null-value + (combiner (term a) + (accumulate combiner null-value term (next a) next b)))) + +(define (product term a next b) + (accumulate * 1 term a next b)) + +(define (factorial n) + (let ((term (lambda (x) x)) + (next (lambda (x) (1+ x)))) + (product term 1 next n))) + +(define (sum term a next b) + (accumulate + 0 term a next b)) + +(define (simpson f a b n) + (let* ((h (/ (- b a) (* 1.0 n))) + (y (lambda (k) (+ a (* k h)))) + (ce (lambda (k) ;coefficient + (cond ((or (= k 0) (= k n)) 1) + ((even? k) 2) + (else 4)))) + (term (lambda (k) + (* (ce k) (f (y k))))) + (next (lambda (k) (1+ k)))) + (* (/ h 3.0) + (sum term 0 next n))))