diff options
author | Masaya Tojo <masaya@tojo.tokyo> | 2024-06-11 02:10:37 +0900 |
---|---|---|
committer | Masaya Tojo <masaya@tojo.tokyo> | 2024-06-11 02:10:37 +0900 |
commit | 552cd6c999f3e44b13be88e45c4a8cb391eb40cf (patch) | |
tree | 15d9cea824cd5627a7c98ba866fb38a1d69457a6 /algebraic-structures.foldable.make.scm | |
parent | 53ccad935b368dbccadd6d64bedb9341f34415c3 (diff) |
Rename filename from `algebraic-structs` to `algebraic-structures`
Diffstat (limited to 'algebraic-structures.foldable.make.scm')
-rw-r--r-- | algebraic-structures.foldable.make.scm | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/algebraic-structures.foldable.make.scm b/algebraic-structures.foldable.make.scm new file mode 100644 index 0000000..46b4dd5 --- /dev/null +++ b/algebraic-structures.foldable.make.scm @@ -0,0 +1,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))) |