-
Notifications
You must be signed in to change notification settings - Fork 0
/
monad-4.scm
51 lines (40 loc) · 1.25 KB
/
monad-4.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
;;; remove the elements of a list that accomplish a predicate and
;;; accummulate the removed elements in another list in correct order
(load "libscm")
(load-option 'format)
(define unit
(lambda (v)
(lambda (k)
(cons k v))))
(define >>=
(lambda (monadic sequel)
(lambda (k/global)
(let ((new/k/global:result (monadic k/global)))
(let ((new/k/global (car new/k/global:result))
(result (cdr new/k/global:result)))
((sequel result) new/k/global))))))
(define remove/predicate
(lambda (l pred?)
(cond ((null? l)
(unit '()))
((pred? (car l))
(>>= (lambda (k)
(cons (lambda (g)
(k (cons (car l) g)))
'__))
(lambda (__)
(remove/predicate (cdr l) pred?))))
(else
(>>= (remove/predicate (cdr l) pred?)
(lambda (d)
(unit (cons (car l) d))))))))
(define k/test
(lambda (l pred?)
(let ((a ((remove/predicate l pred?)
(lambda (x) x))))
(let ((k (car a))
(res (cdr a)))
(test l (k '()) res)))))
(k/test '(1 3 5 7 9) odd?)
(k/test '(2 4 6 8) odd?)
(k/test '(1 2 3 4 5 6 7 8 9) odd?)