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