-
Notifications
You must be signed in to change notification settings - Fork 0
/
little-monads-maybe.rkt
72 lines (58 loc) · 1.69 KB
/
little-monads-maybe.rkt
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
62
63
64
65
66
67
68
69
70
#lang racket
;;; Make experiments using
;;;
;;; A Schemer’s View of Monads
;;; Daniel P. Friedman & Adam C. Foltzer
;;; 2010, 2011
;;; MAYBE MONAD
(define unit
(lambda (a)
(cons a '_) ; <= This is a monad. It's a pair.
))
(define star
(lambda (sequel)
(lambda (ma)
(if (eq? (cdr ma) '_) ; <= This is a monad. It's a pair.
(let* ((pure (car ma)))
(sequel pure))
ma))))
;;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(define assq
(lambda (v a)
(cond ((null? a)
'(_ . not-found))
((eq? (caar a) v)
(unit (cdar a)))
(else
((star (lambda (x) (unit x)))
(assq v (cdr a)))))))
(module+ test
(assq 10 '((1 . 10) (2 . 20) (3 . 30) (10 . 50)))
(assq 11 '((1 . 10) (2 . 20) (3 . 30) (10 . 50))))
;;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(define unit2
(lambda (a)
(lambda () ; <= This is a monad-A. It's a thunk.
(cons a '_)
)))
(define star2
(lambda (sequel)
(lambda (ma)
(lambda () ; <= This is a monad-A. It's a thunk.
(let ((pair (ma)))
(define-values (pure extra) (values (car pair) (cdr pair)))
(if (eq? extra '_)
(sequel pure)
pair))))))
(define assq2
(lambda (v a)
(cond ((null? a)
(lambda ()
'(_ . not-found)))
((eq? (caar a) v)
(unit2 (cdar a)))
(else
((star2 (lambda (x) (unit x)))
(assq2 v (cdr a)))))))
((assq2 10 '((1 . 10) (2 . 20) (3 . 30) (10 . 50) (20 . 44))))
((assq2 11 '((1 . 10) (2 . 20) (3 . 30) (10 . 50))))