aboutsummaryrefslogtreecommitdiff
path: root/algebraic-structures.applicative.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.applicative.scm
parent653b204b583da363a97464960a00f1bd0dbed865 (diff)
Rename modules from (<feature name> ... make) to (<feature name>)
Diffstat (limited to 'algebraic-structures.applicative.scm')
-rw-r--r--algebraic-structures.applicative.scm28
1 files changed, 28 insertions, 0 deletions
diff --git a/algebraic-structures.applicative.scm b/algebraic-structures.applicative.scm
new file mode 100644
index 0000000..ac3028a
--- /dev/null
+++ b/algebraic-structures.applicative.scm
@@ -0,0 +1,28 @@
+(functor ((algebraic-structures applicative) (A (pure map map2)))
+ (pure map map2 map* apply)
+ (import (rename scheme (map scheme:map) (apply scheme:apply))
+ (only (chicken base) sub1 add1 foldl case-lambda)
+ A
+ matchable)
+
+ (define (curry-n f n)
+ (let rec ((i n)
+ (k (lambda (args)
+ (scheme:apply f args))))
+ (if (= i 1)
+ (lambda (x) (k (list x)))
+ (lambda (x)
+ (rec (sub1 i)
+ (lambda (args)
+ (k (cons x args))))))))
+
+ (define map*
+ (case-lambda
+ ((f x) (map f x))
+ ((f x y) (map2 f x y))
+ ((f x . xs)
+ (let ((g (curry-n f (add1 (length xs)))))
+ (foldl apply (apply (pure g) x) xs)))))
+
+ (define (apply a1 a2)
+ (map2 (lambda (f x) (f x)) a1 a2)))