aboutsummaryrefslogtreecommitdiff
path: root/algebraic-structs.foldable.vector.base.scm
diff options
context:
space:
mode:
authorMasaya Tojo <masaya@tojo.tokyo>2024-06-09 05:13:44 +0900
committerMasaya Tojo <masaya@tojo.tokyo>2024-06-09 05:13:44 +0900
commit92eeab2815eae6cd9ef22530d2e50fe0a620ec46 (patch)
tree8bfdca868c360846829f7582e12ab77ce843b8eb /algebraic-structs.foldable.vector.base.scm
Initial commit
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)))))))