-
Notifications
You must be signed in to change notification settings - Fork 0
/
monad-7.scm
57 lines (49 loc) · 1.31 KB
/
monad-7.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
;;; remove odd elements and returns a list ((pos1 . even-1) (pos2 . even-2))
(define unit
(lambda (v)
(lambda (pos rem-odd)
(lambda (col)
(col pos rem-odd v)))))
(define >>=
(lambda (ma sequel)
(lambda (n o)
((ma n o)
(lambda (n o v)
((sequel v)
n o))))))
(define rem
(lambda (l)
(cond ((null? l)
(unit '()))
((odd? (car l))
(>>= (lambda (pos rem-odd)
(lambda (col)
(col (+ pos 1)
(+ rem-odd 1)
'_)))
(lambda (_)
(>>= (rem (cdr l))
(lambda (d)
(unit d))))))
(else
(>>= (lambda (pos rem-odd)
(lambda (col)
(col (+ pos 1)
rem-odd
(cons pos (car l)))))
(lambda (a)
(>>= (rem (cdr l))
(lambda (d)
(unit (cons a d))))))))))
(((rem '()) 0 0)
(lambda (n m v)
(display "\n::")
(display (list n m v))
(newline)))
(((rem '(1 3 5 7)) 1 0)
(lambda (n m v)
(display (list n m v))))
(((rem '(1 2 3 4 5 7 8 9 0)) 1 0)
(lambda (n m v)
(newline)
(display (list n m v))))