-
Notifications
You must be signed in to change notification settings - Fork 0
/
monad-9.scm
97 lines (82 loc) · 2.12 KB
/
monad-9.scm
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
(load "libscm")
(define unit
(lambda (a)
;; (__d ":" a)
(lambda (k __ex__)
(k a))))
(define bind
(lambda (ma seq)
(lambda (k ex)
(ma (lambda (p)
((seq p) k ex))
ex))))
(define product
(lambda (l)
(cond ((null? l)
(unit 1))
((pair? (car l))
(bind (product (cdr l))
(lambda (d)
;; (__d "<:A" (car l))
(bind (product (car l))
(lambda (a)
;; (__d "<:B" (car l))
(unit (* a d)))))))
((pair? l)
(bind (lambda (k ex)
(if (zero? (car l))
(ex)
((product (cdr l)) k ex)))
(lambda (p)
(__d "<:C" (car l))
(unit (* (car l) p))))))))
;;; (10 20 30)
(define return
(lambda (a)
(lambda (k)
(k a))))
(define >>=
(lambda (ma seq)
(lambda (k)
(ma (lambda (p)
((seq p) k))))))
(display
((>>= (lambda (k)
((>>= (lambda (k)
((>>= (lambda (k)
((>>= (lambda (k)
((>>= (lambda (k)
((return 1) k))
(lambda (p)
(return (* 6 p))))
k ))
(lambda (p)
(return (* 5 p))))
k ))
(lambda (p)
(return (* 4 p))))
k ))
(lambda (p)
(return (* 3 p))))
k ))
(lambda (p)
(return (* 2 p))))
(lambda (x)
x)))
(newline)
(define test
(lambda (input)
(display "test: ")
(display input)
(newline)
((product input)
(lambda (x)
(display "==>")
(display x)
(newline))
(lambda ()
(display "\nstop\n")))))
(test '())
(test '(10 20 30))
(test '(1 2 (3 (4 5) 6) 7))
(test '(1 2 (3 (4 0) 6) 7))