aboutsummaryrefslogtreecommitdiff
path: root/algebraic-structures.foldable.vector.base.scm
blob: 0c5af898a46731a83e40a1b626724002fc36168b (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-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)))))))