aboutsummaryrefslogtreecommitdiff
path: root/data/monoid.scm
diff options
context:
space:
mode:
Diffstat (limited to 'data/monoid.scm')
-rw-r--r--data/monoid.scm25
1 files changed, 25 insertions, 0 deletions
diff --git a/data/monoid.scm b/data/monoid.scm
new file mode 100644
index 0000000..45d30cd
--- /dev/null
+++ b/data/monoid.scm
@@ -0,0 +1,25 @@
+(define-module (data monoid)
+ #:use-module (oop goops)
+ #:use-module (srfi srfi-1)
+ #:export (null mappend <>))
+
+(define-generic null)
+(define-generic mappend)
+
+(define (<> . args)
+ (fold mappend (null (car args)) (reverse args)))
+
+;;; Lists
+
+(define-method (mappend (a <pair>) (b <pair>))
+ (append a b))
+(define-method (mappend (a <pair>) (b <null>)) a)
+(define-method (mappend (a <null>) (b <pair>)) b)
+(define-method (mappend (a <null>) (b <null>)) '())
+(define-method (null (a <pair>)) '())
+(define-method (null (a <null>)) '())
+
+;;; Strings
+(define-method (mappend (a <string>) (b <string>))
+ (string-append a b))
+(define-method (null (a <string>)) "")