summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2019-11-22 17:07:12 -0500
committerrsiddharth <s@ricketyspace.net>2019-11-22 17:07:12 -0500
commit16eedb8d4db0a3c3cef797eaa52564d53a2f6af8 (patch)
treecb6ad1e63ea8e78c85dd0abcf9acf786158fbd79
parente175ae249db5fad445395f244f6cd0b4e497c004 (diff)
net: Add (net ricketyspace sicp two thirtyfour).
-rw-r--r--net/ricketyspace/sicp/two/thirtyfour.scm25
1 files changed, 25 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/two/thirtyfour.scm b/net/ricketyspace/sicp/two/thirtyfour.scm
new file mode 100644
index 0000000..0f6f6f3
--- /dev/null
+++ b/net/ricketyspace/sicp/two/thirtyfour.scm
@@ -0,0 +1,25 @@
+;;;; License: CC0-1.0
+
+(define-module (net ricketyspace sicp two thirtyfour)
+ #:export (horner-eval))
+
+
+(define (accumulate op initial sequence)
+ (if (null? sequence)
+ initial
+ (op (car sequence)
+ (accumulate op initial (cdr sequence)))))
+
+
+(define (horner-eval x coefficient-sequence)
+ (accumulate (λ (this-coeff higher-terms)
+ (+ this-coeff (* higher-terms x)))
+ 0
+ coefficient-sequence))
+
+;;; Guile REPL
+;;;
+;;; scheme@(guile-user)> ,use (net ricketyspace sicp two thirtyfour)
+;;; scheme@(guile-user)> (horner-eval 2 (list 1 3 0 5 0 1))
+;;; $2 = 79
+;;;