aboutsummaryrefslogtreecommitdiff
path: root/algebraic-structures.foldable.vector.base.scm
blob: 90d06da82146c10e904d477989c4c293c668683c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(module (algebraic-structures 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)))))))