diff options
author | Masaya Tojo <masaya@tojo.tokyo> | 2024-06-14 00:27:56 +0900 |
---|---|---|
committer | Masaya Tojo <masaya@tojo.tokyo> | 2024-06-14 00:27:56 +0900 |
commit | 0010505cc0429922870de5cfb3e0721e09788c59 (patch) | |
tree | 4010439b8a8b5f82c706a1df608ce7413c850f18 /examples | |
parent | c9ea9a8750e6b7cca2d285d6c28a398182099a7d (diff) |
Fix example file `optional.scm`
Diffstat (limited to 'examples')
-rw-r--r-- | examples/optional.scm | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/examples/optional.scm b/examples/optional.scm index a15fe16..ea2be96 100644 --- a/examples/optional.scm +++ b/examples/optional.scm @@ -1,3 +1,7 @@ +(import (algebraic-structures functor) + (algebraic-structures applicative) + (algebraic-structures monad)) + (module (data optional) (<some> some some? some-value <none> none none?) (import scheme (chicken base) @@ -20,15 +24,25 @@ (lambda (_ out) (fprintf out "#<(none)>")))) -(module (data optional monad base) (pure map map2 >>=) - (import (except scheme map) +(module (data optional functor) = (algebraic-structures functor) + (import scheme (prefix (data optional) opt:) - matchable) + matchable + (chicken module)) + (export map1) - (define (map f opt) + (define (map1 f opt) (match opt [($ opt:<some> x) (opt:some (f x))] - [($ opt:<none>) (opt:none)])) + [($ opt:<none>) (opt:none)]))) + +(module (data optional applicative) = (algebraic-structures applicative) + (import scheme + (prefix (data optional) opt:) + matchable + (chicken module)) + (reexport (data optional functor)) + (export pure map2) (define (pure x) (opt:some x)) @@ -39,31 +53,30 @@ (match opt2 [($ opt:<some> y) (opt:some (f x y))] [($ opt:<none>) (opt:none)])] - [($ opt:<none>) (opt:none)])) + [($ opt:<none>) (opt:none)]))) + +(module (data optional monad) = (algebraic-structures monad) + (import (except scheme map apply) + (prefix (data optional) opt:) + matchable + (chicken module)) + (reexport (data optional applicative)) + (export >>=) (define (>>= opt f) (match opt [($ opt:<some> x) (f x)] [($ opt:<none>) (opt:none)]))) -(import (only (algebraic-structures functor)) - (only (algebraic-structures applicative)) - (only (algebraic-structures monad))) - -(module (data optional functor) = ((algebraic-structures functor) (data optional monad base))) -(module (data optional applicative) = ((algebraic-structures applicative) (data optional monad base))) -(module (data optional monad) = ((algebraic-structures monad) (data optional monad base))) - -(import (prefix (data optional) opt:) - (prefix (data optional functor) opt:) - (prefix (data optional applicative) opt:) - (prefix (data optional monad) opt:)) +(import (prefix (data optional) opt:)) +(import (prefix (data optional applicative) opt:)) +(import (prefix (data optional monad) opt:)) ;; (opt:map (lambda (x) (* x x)) (opt:pure 5)) => (some 25) ;; (opt:map (lambda (x) (* x x)) (opt:none)) => (none) -;; (opt:map* + (opt:pure 1) (opt:pure 2) (opt:pure 3)) => (some 6) -;; (opt:map* + (opt:pure 1) (opt:none) (opt:pure 3)) => (none) +;; (opt:map + (opt:pure 1) (opt:pure 2) (opt:pure 3)) => (some 6) +;; (opt:map + (opt:pure 1) (opt:none) (opt:pure 3)) => (none) ;; (opt:do (x <- (opt:pure 3)) ;; (y <- (opt:pure 4)) |