aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2018-11-13 00:00:06 +0100
committerHugo Hörnquist <hugo@hornquist.se>2018-11-13 00:00:06 +0100
commit7481b6bd5192997c87b3762dae3213b48b897ddb (patch)
tree5d3b7cc422d16376bf9b22fb3ae219d2e76374ba /examples
parentAdditional comment in state-minimal. (diff)
downloadscheme-monad-7481b6bd5192997c87b3762dae3213b48b897ddb.tar.gz
scheme-monad-7481b6bd5192997c87b3762dae3213b48b897ddb.tar.xz
Move examples.scm to directory.
Diffstat (limited to 'examples')
-rw-r--r--examples/do.scm41
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/do.scm b/examples/do.scm
new file mode 100644
index 0000000..d758177
--- /dev/null
+++ b/examples/do.scm
@@ -0,0 +1,41 @@
+(add-to-load-path (dirname (current-filename)))
+
+(use-modules (control monad)
+ (data monoid)
+ (data optional)
+ (data writer))
+
+;;; Optional Monad, and do notation
+
+(do x <- (just 10)
+ x) ; => 10
+
+(do let y = (just 10)
+ x <- y
+ (+ x 5)) ; => 15
+
+(do x <- (nothing)
+ (+ x 5)) ; => [Nothing]
+
+(do x <- (just 10)
+ y <- (just 20)
+ (+ x y)) ; => 30
+
+(do (just 10)
+ (nothing)
+ (just 20)) ; => [Nothing]
+
+;;; Writer Monad, and do notation
+
+;; Int -> Writer Int String
+(define (log-number n)
+ (writer n (format #f "Got nuber: ~a" n)))
+
+(do a <- (log-number 3)
+ b <- (log-number 5)
+ (writer (* a b) ""))
+;; => [Writer 15, "Got nuber: 3Got nuber: 5"]
+
+(do (log-number 3)
+ (log-number 5))
+;; => [Writer 5, "Got nuber: 3Got nuber: 5"]