summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2019-12-21 18:52:07 -0500
committerrsiddharth <s@ricketyspace.net>2019-12-21 18:52:07 -0500
commit19efa1a78b2c15f64a1e186f2a6eebfad7d75db2 (patch)
treeb33116a6f32c8937ffd325c35769ccb9be693a4e /net/ricketyspace
parent242cbda33c815d2a0d11e247e6f7a799e2764bdd (diff)
Add (net ricketyspace sicp two thirtyeight).
Diffstat (limited to 'net/ricketyspace')
-rw-r--r--net/ricketyspace/sicp/two/thirtyeight.scm47
1 files changed, 47 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/two/thirtyeight.scm b/net/ricketyspace/sicp/two/thirtyeight.scm
new file mode 100644
index 0000000..40b4a13
--- /dev/null
+++ b/net/ricketyspace/sicp/two/thirtyeight.scm
@@ -0,0 +1,47 @@
+;;;; License: CC0-1.0
+
+(define-module (net ricketyspace sicp two thirtyeight)
+ #:export (fold-left
+ fold-right))
+
+(define (accumulate op initial sequence)
+ (if (null? sequence)
+ initial
+ (op (car sequence)
+ (accumulate op initial (cdr sequence)))))
+
+
+(define (fold-right op initial sequence)
+ (accumulate op initial sequence))
+
+
+(define (fold-left op initial sequence)
+ (define (iter result rest)
+ (if (null? rest)
+ result
+ (iter (op result (car rest))
+ (cdr rest))))
+ (iter initial sequence))
+
+;;; Guile REPL
+;;;
+;;; scheme@(guile-user)> ,use (net ricketyspace sicp two thirtyeight)
+;;; scheme@(guile-user)> (fold-right / 1.0 (list 1 2 3))
+;;; $2 = 1.5
+;;; scheme@(guile-user)> (fold-left / 1.0 (list 1 2 3))
+;;; $12 = 0.16666666666666666
+;;; scheme@(guile-user)> (fold-right list '() (list 1 2 3))
+;;; $13 = (1 (2 (3 ())))
+;;; scheme@(guile-user)> (fold-left list '() (list 1 2 3))
+;;; $14 = (((() 1) 2) 3)
+;;; scheme@(guile-user)> (fold-right * 1 (list 1 2 3))
+;;; $19 = 6
+;;; scheme@(guile-user)> (fold-left * 1 (list 1 2 3))
+;;; $20 = 6
+;;;
+;;;
+;;; Conclusion: When the result produced by applying `op` on
+;;; `sequence` is independent of the order in which `op` is applied to
+;;; each element of the `sequence` then fold-right and fold-left will
+;;; yield the same result.
+;;;