diff options
Diffstat (limited to 'examples.scm')
-rw-r--r-- | examples.scm | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/examples.scm b/examples.scm new file mode 100644 index 0000000..a2f2319 --- /dev/null +++ b/examples.scm @@ -0,0 +1,61 @@ +;; (read-enable 'curly-infix) + +;;; Examples: +;;; These are do in the conext of the optional monad. + +(add-to-load-path (dirname (current-filename))) + +(use-modules (control monad) + (data monoid) + (data optional) + (data writer)) + +(do (just x) <- (just 10) + x) ; => 10 + +(do let y = (just 10) + (just x) <- y + (+ x 5)) ; => 15 + +(do (just x) <- (nothing) + (+ x 5)) ; => [Nothing] + +;;; + +(do let either = left 10 + (left x) <- either + x) ; EVALUATION ERROR + +;; Int -> Writer Int String +(define (log-number n) + (writer n (format #f "Got nuber: ~a" n))) + +(define (mult-with-log) + (do (writer a) <- (log-number 3) + (writer b) <- (log-number 5) + (* a b))) + +(do (writer a) <- (log-number 1) + a) ; EVALUATION ERROR + +(begin { (log-number 1) >>= log-number }) +;; => [Writer 1, "Got nuber: 1, Got nuber: 1"] + +(do (writer a) <- (log-number 3) + (writer b) <- (log-number 5) + (writer (* a b) "")) +;; EVALUATION ERROR +;; => [Writer 3, "Got nuber: 3, base"] + +(log-number 3) ; => [Writer 3, "Got nuber: 3"] +(just 1) ; => [Just 1] + +(do let y = 5 + (just x) <- (just 10) + (just (* x y))) +;; => [Just 50] + +;;; TODO +;;; '<- and 'let can't be used after '<- + + |