diff options
author | Masaya Tojo <masaya@tojo.tokyo> | 2024-06-11 02:10:37 +0900 |
---|---|---|
committer | Masaya Tojo <masaya@tojo.tokyo> | 2024-06-11 02:10:37 +0900 |
commit | 552cd6c999f3e44b13be88e45c4a8cb391eb40cf (patch) | |
tree | 15d9cea824cd5627a7c98ba866fb38a1d69457a6 /algebraic-structures.foldable.vector.base.scm | |
parent | 53ccad935b368dbccadd6d64bedb9341f34415c3 (diff) |
Rename filename from `algebraic-structs` to `algebraic-structures`
Diffstat (limited to 'algebraic-structures.foldable.vector.base.scm')
-rw-r--r-- | algebraic-structures.foldable.vector.base.scm | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/algebraic-structures.foldable.vector.base.scm b/algebraic-structures.foldable.vector.base.scm new file mode 100644 index 0000000..0c5af89 --- /dev/null +++ b/algebraic-structures.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))))))) |