aboutsummaryrefslogtreecommitdiff
path: root/examples.scm
blob: a2f2319eb031c145827ed95d8b71e4b2797e2753 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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 '<-