summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace/sicp/two/thirtyfour.scm
diff options
context:
space:
mode:
Diffstat (limited to 'net/ricketyspace/sicp/two/thirtyfour.scm')
-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
+;;;