From 16eedb8d4db0a3c3cef797eaa52564d53a2f6af8 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Fri, 22 Nov 2019 17:07:12 -0500 Subject: net: Add (net ricketyspace sicp two thirtyfour). --- net/ricketyspace/sicp/two/thirtyfour.scm | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 net/ricketyspace/sicp/two/thirtyfour.scm (limited to 'net/ricketyspace') 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 +;;; -- cgit v1.2.3