From ab601b9f1fd150c249cf3e8aade6c904d49562d0 Mon Sep 17 00:00:00 2001 From: Masaya Tojo Date: Mon, 10 Jun 2024 01:05:27 +0900 Subject: Add example links in README.md --- README.md | 78 ++++----------------------------------------------------------- 1 file changed, 4 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index d0cd6f3..9dadc45 100644 --- a/README.md +++ b/README.md @@ -29,79 +29,9 @@ $ chicken-install - Alternative - list -## Example: `Optional` monad +## Example -``` -(module (data optional) ( some some? some-value none none?) - (import scheme (chicken base) (chicken format)) - - (define-record-type - (some value) - some? - (value some-value)) - - (set! (record-printer ) - (lambda (x out) - (fprintf out "#<(some ~S)>" (some-value x)))) - - (define-record-type - (none) - none?) - - (set! (record-printer ) - (lambda (_ out) - (fprintf out "#<(none)>")))) - -(module (data optional monad base) (pure map map2 >>=) - (import (except scheme map) - (prefix (data optional) opt:) - matchable) - - (define (map f opt) - (match opt - [($ opt: x) (opt:some (f x))] - [($ opt:) (opt:none)])) - - (define (pure x) - (opt:some x)) - - (define (map2 f opt1 opt2) - (match opt1 - [($ opt: x) - (match opt2 - [($ opt: y) (opt:some (f x y))] - [($ opt:) (opt:none)])] - [($ opt:) (opt:none)])) - - (define (>>= opt f) - (match opt - [($ opt: x) (f x)] - [($ opt:) (opt:none)]))) - -(import (only (algebraic-structs functor make))) -(import (only (algebraic-structs applicative make))) -(import (only (algebraic-structs monad make))) -(module (data optional functor) = ((algebraic-structs functor make) (data optional monad base))) -(module (data optional applicative) = ((algebraic-structs applicative make) (data optional monad base))) -(module (data optional monad) = ((algebraic-structs monad make) (data optional monad base))) - -(import (prefix (data optional) opt:) - (prefix (data optional functor) opt:) - (prefix (data optional applicative) opt:) - (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:do (x <- (opt:pure 3)) -;; (y <- (opt:pure 4)) -;; (opt:pure (+ x y))) -;; => (some 7) -``` - -## LICENSE +- Monad + - [optonal](./examples/optional.scm) + - [state](./examples/optional.scm) -This program is licensed under the MIT License. See the LICENSE file for details. -- cgit v1.2.3