summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2017-09-30 21:19:27 +0000
committerrsiddharth <s@ricketyspace.net>2017-09-30 21:19:27 +0000
commit3e3f894460e5c5da45c645c5dd937bbbc62ebadf (patch)
treeee599202084d4b64e302ba176a9b0fd26a9baff5
parentb0170945a60133e0468161e069b3d817502f9091 (diff)
net: Add (net ricketyspace sicp one thirty).
-rw-r--r--net/ricketyspace/sicp/one/thirty.scm28
1 files changed, 28 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/one/thirty.scm b/net/ricketyspace/sicp/one/thirty.scm
new file mode 100644
index 0000000..48269f5
--- /dev/null
+++ b/net/ricketyspace/sicp/one/thirty.scm
@@ -0,0 +1,28 @@
+;;;; Under Creative Commons Attribution-ShareAlike 4.0
+;;;; International. See
+;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.
+
+(define-module (net ricketyspace sicp one thirty)
+ #:export (sum))
+
+(define (sum term a next b)
+ (define (iter a result)
+ (if (> a b)
+ result
+ (iter (next a) (+ (term a) result))))
+ (iter a 0))
+
+;;; GNU Guile REPL
+;;;
+;;; scheme@(guile-user)> ,use (net ricketyspace sicp one thirty)
+;;; scheme@(guile-user)> (define (inc n) (+ n 1))
+;;; scheme@(guile-user)> (define (cube x) (* x x x))
+;;; scheme@(guile-user)> (sum cube 1 inc 10)
+;;; scheme@(guile-user)> (define (pi-sum a b)
+;;; (define (pi-term x)
+;;; (/ 1.0 (* x (+ x 2))))
+;;; (define (pi-next x) (+ x 4))
+;;; (sum pi-term a pi-next b))
+;;; scheme@(guile-user)> (* 8 (pi-sum 1 1000))
+;;; $9 = 3.139592655589782
+;;;