blob: a4af0ca8d46602d68d67e46993a9efd71db0702d (
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
40
41
42
43
44
45
46
47
48
49
50
|
(functor ((algebraic-structures foldable) (F (fold)))
(fold
length
count
any
every
member?)
(import (except scheme length)
(only F fold)
(only (chicken base) add1 call/cc assert))
(define (length xs)
(fold (lambda (_ acc) (add1 acc))
0
xs))
(define (count p? xs)
(fold (lambda (e acc)
(if (p? e)
(add1 acc)
acc))
0
xs))
(define (any pred xs)
(call/cc
(lambda (return)
(fold (lambda (e acc)
(cond ((pred e) => return)
(else acc)))
#f
xs))))
(define (every pred xs)
(call/cc
(lambda (return)
(fold (lambda (e acc)
(or (pred e) (return #f)))
#t
xs))))
(define (member? x xs #!optional (= equal?))
(call/cc
(lambda (return)
(fold (lambda (e _)
(if (= e x)
(return #t)
#f))
#f
xs)))))
|