From 6f4601d5bd245e99831cedb12b732d77501a16e1 Mon Sep 17 00:00:00 2001 From: Masaya Tojo Date: Sun, 16 Jun 2024 18:39:20 +0900 Subject: Add usages section to README.md --- examples/mod7.scm | 51 ++++++++++++++++++++++++++++++++++++++++ examples/pythagorean-triples.scm | 11 +++++++++ 2 files changed, 62 insertions(+) create mode 100644 examples/mod7.scm create mode 100644 examples/pythagorean-triples.scm (limited to 'examples') diff --git a/examples/mod7.scm b/examples/mod7.scm new file mode 100644 index 0000000..cfdce61 --- /dev/null +++ b/examples/mod7.scm @@ -0,0 +1,51 @@ +(import (algebraic-structures semigroup) + (algebraic-structures monoid) + (algebraic-structures group) + (algebraic-structures monoid fold) + (algebraic-structures list foldable)) + +(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)))) + +(module (mod7 fold) = ((algebraic-structures monoid fold) (mod7 monoid) (algebraic-structures list foldable))) + +(import (prefix (mod7 group) mod7:) + (prefix (mod7 fold) mod7:)) diff --git a/examples/pythagorean-triples.scm b/examples/pythagorean-triples.scm new file mode 100644 index 0000000..4f620af --- /dev/null +++ b/examples/pythagorean-triples.scm @@ -0,0 +1,11 @@ +(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)))) -- cgit v1.2.3