aboutsummaryrefslogtreecommitdiff
path: root/algebraic-structs.foldable.vector.base.scm
diff options
context:
space:
mode:
Diffstat (limited to 'algebraic-structs.foldable.vector.base.scm')
-rw-r--r--algebraic-structs.foldable.vector.base.scm21
1 files changed, 21 insertions, 0 deletions
diff --git a/algebraic-structs.foldable.vector.base.scm b/algebraic-structs.foldable.vector.base.scm
new file mode 100644
index 0000000..0c5af89
--- /dev/null
+++ b/algebraic-structs.foldable.vector.base.scm
@@ -0,0 +1,21 @@
+(module (algebraic-structs foldable vector base) (foldl foldr)
+ (import scheme
+ (only (chicken base) add1 sub1))
+
+ (define (foldl f z v)
+ (let ((len (vector-length v)))
+ (let loop ((i 0)
+ (acc z))
+ (if (= i len)
+ acc
+ (loop (add1 i)
+ (f acc (vector-ref v i)))))))
+
+ (define (foldr f z v)
+ (let ((len (vector-length v)))
+ (let loop ((i (sub1 len))
+ (acc z))
+ (if (< i 0)
+ acc
+ (loop (sub1 i)
+ (f (vector-ref v i) acc)))))))