aboutsummaryrefslogtreecommitdiff
path: root/algebraic-structs.foldable.make.scm
blob: 46b4dd5d99ccdd981e350d87bfa2bc2626f207d7 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(functor ((algebraic-structs foldable make) (F (foldl foldr)))
    (foldl foldr length find any every ->list)
  (import (except scheme length) F
          (only (chicken base) add1 call/cc))

  (define (length xs)
    (foldl (lambda (acc _) (add1 acc))
           0
           xs))

  (define (find p? xs)
    (call/cc
     (lambda (k)
       (foldl (lambda (acc e)
                (if (p? e)
                    (k e)
                    acc))
              #f
              xs))))

  (define (any pred xs)
    (call/cc
     (lambda (return)
       (foldl (lambda (acc e)
                (cond ((pred e) => return)
                      (else acc)))
              #f
              xs))))

  (define (every pred xs)
    (call/cc
     (lambda (return)
       (foldl (lambda (acc e)
                (or (pred e) (return #f)))
              #t
              xs))))

  (define (->list xs)
    (foldr cons '() xs)))