aboutsummaryrefslogtreecommitdiff
path: root/algebraic-structures.foldable.scm
diff options
context:
space:
mode:
authorMasaya Tojo <masaya@tojo.tokyo>2024-06-13 02:11:07 +0900
committerMasaya Tojo <masaya@tojo.tokyo>2024-06-13 02:11:07 +0900
commitca1584a5c87c2952af08c74ce80b1cb2a75a1d19 (patch)
tree6a6f5ebde7bb00f8bd74501ecac563c5a1dc51f6 /algebraic-structures.foldable.scm
parent653b204b583da363a97464960a00f1bd0dbed865 (diff)
Rename modules from (<feature name> ... make) to (<feature name>)
Diffstat (limited to 'algebraic-structures.foldable.scm')
-rw-r--r--algebraic-structures.foldable.scm39
1 files changed, 39 insertions, 0 deletions
diff --git a/algebraic-structures.foldable.scm b/algebraic-structures.foldable.scm
new file mode 100644
index 0000000..3ab1f6e
--- /dev/null
+++ b/algebraic-structures.foldable.scm
@@ -0,0 +1,39 @@
+(functor ((algebraic-structures foldable) (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)))