summaryrefslogtreecommitdiff
path: root/coding-exercises/1/32.rkt
diff options
context:
space:
mode:
authorMike Vink <mike1994vink@gmail.com>2023-03-03 21:53:57 +0100
committerMike Vink <mike1994vink@gmail.com>2023-03-03 21:53:57 +0100
commit035be9b1895133e0ffd1afdcc3a59c5d84c4b8d8 (patch)
treee1fa4a22db691f145b14ee6a3c8e49063ecae2bf /coding-exercises/1/32.rkt
parent0988f106514e61b59f53e7fe3c599e03edcfd47c (diff)
fixup
Diffstat (limited to 'coding-exercises/1/32.rkt')
-rw-r--r--coding-exercises/1/32.rkt38
1 files changed, 38 insertions, 0 deletions
diff --git a/coding-exercises/1/32.rkt b/coding-exercises/1/32.rkt
new file mode 100644
index 0000000..21a557c
--- /dev/null
+++ b/coding-exercises/1/32.rkt
@@ -0,0 +1,38 @@
+#lang racket
+
+(define (accumulate combiner null-value term a next b)
+ (define (iter a result)
+ (if (> a b)
+ result
+ (iter
+ (next a)
+ (combiner
+ (term a)
+ result))))
+ (iter null-value a))
+
+(define (accumulate combiner null-value term a next b)
+ (if (> a b)
+ null-value
+ (combiner
+ (term a)
+ (accumulate
+ combiner
+ null-value
+ term (next a) next b))))
+
+
+(define (sum term a next b)
+ (accumulate
+ (lambda (x y) (+ x y))
+ 0
+ term a next b))
+
+(define (product term a next b)
+ (accumulate
+ (lambda (x y) (* x y))
+ 1
+ term a next b))
+
+(sum id 0 inc 8)
+(product id 1 inc 3)