-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlittle-monad-filter.rkt
71 lines (60 loc) · 1.78 KB
/
little-monad-filter.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
71
#lang racket/base
(define unit
(lambda (v)
(lambda (state)
(cons v state))))
(define pipe
(lambda (ma sequel)
(lambda (state)
(let ((next/state (ma state)))
(let ((a (car next/state))
(b (cdr next/state)))
(let ((mb (sequel a)))
(mb b)))))))
(define filter
(lambda (l p?)
(cond ((null? l) (unit '()))
((pair? (car l))
(pipe (filter (car l) p?)
(lambda (v1)
(filter (cdr l) p?)
(lambda (v2)
(unit (cons v1 v2))))))
((p? (car l))
(pipe (filter (cdr l) p?)
(lambda (v)
(unit (cons (car l) v)))))
(else
(pipe (filter (cdr l) p?) unit)))))
((filter '(1 2 3) odd?) 'not-used)
(define unit2
(lambda (x y)
(lambda (state)
(cons (cons x y) state))))
(define pipe2
(lambda (ma sequel)
(lambda (state)
(let ((next-state (ma state)))
(let ((a (caar next-state))
(b (cdar next-state))
(s (cdr next-state)))
(let ((mb (sequel a b)))
(mb s)))))))
(define split
(lambda (l p?)
(cond ((null? l)
(unit2 '() '()))
((pair? (car l))
(pipe2 (split (car l) p?)
(lambda (a1 a2)
(pipe2 (split (cdr l) p?)
(lambda (d1 d2)
(unit2 (cons a1 d1) (cons a2 d2)))))))
((p? (car l))
(pipe2 (split (cdr l) p?)
(lambda (a d)
(unit2 (cons (car l) a) d))))
(else (pipe2 (split (cdr l) p?)
(lambda (a d)
(unit2 a (cons (car l) d))))))))
((split '(1 2 3 4 5) odd?) 'x)