aboutsummaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md85
1 files changed, 85 insertions, 0 deletions
diff --git a/README.md b/README.md
index 7152615..cc112db 100644
--- a/README.md
+++ b/README.md
@@ -11,6 +11,91 @@ $ cd algebraic-structures
$ chicken-install
```
+## Usage
+
+### Define Semigroup, Monoid and Group
+
+```scheme
+(import (algebraic-structures semigroup)
+ (algebraic-structures monoid)
+ (algebraic-structures group))
+
+(module (mod7 semigroup) = (algebraic-structures semigroup)
+ (import scheme
+ (chicken module)
+ (chicken base))
+ (export <>)
+
+ (define (<> x y)
+ (assert (integer? x))
+ (assert (integer? y))
+ (assert (not (zero? x)))
+ (assert (not (zero? y)))
+ (modulo (* x y) 7)))
+
+(module (mod7 monoid) = (algebraic-structures monoid)
+ (import scheme
+ (chicken module)
+ (chicken base))
+ (reexport (mod7 semigroup))
+ (export unit)
+
+ (define unit 1))
+
+(module (mod7 group) = (algebraic-structures group)
+ (import scheme
+ (chicken base)
+ (chicken module)
+ matchable)
+ (reexport (mod7 monoid))
+ (export inv)
+
+ (define (inv n)
+ (assert (integer? n))
+ (assert (not (zero? n)))
+ (match (modulo n 7)
+ (1 1)
+ (2 4)
+ (3 5)
+ (4 2)
+ (5 3)
+ (6 6))))
+
+(import (prefix (mod7 group) mod7:))
+```
+
+In REPL:
+
+```
+> (map (cut mod7:pow 3 <>) '(0 1 2 3 4 5 6 7 8 9 10 11))
+(1 3 2 6 4 5 1 3 2 6 4 5)
+> (mod7:fold '(1 2 3 4 5 6))
+6
+```
+
+### Monad Syntax
+
+```scheme
+(import (srfi 41)
+ (prefix (algebraic-structures stream monad) stream:)
+ (prefix (algebraic-structures stream alternative) stream:))
+
+(define (pythagorean-triples)
+ (stream:do (b <- (stream-from 1))
+ (a <- (stream-range 1 b))
+ (let c^2 = (+ (* a a) (* b b)))
+ (let-values (c r) = (exact-integer-sqrt c^2))
+ (stream:guard (zero? r))
+ (stream (list a b c))))
+```
+
+In REPL:
+
+```
+> (stream->list (stream-take 10 (pythagorean-triples)))
+((3 4 5) (6 8 10) (5 12 13) (9 12 15) (8 15 17) (12 16 20) (15 20 25) (20 21 29) (7 24 25) (10 24 26))
+```
+
## Supported Features
- Semigroup: `(algebraic-structures semigroup)`