-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlittle-monad-7.rkt
46 lines (40 loc) · 1.2 KB
/
little-monad-7.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
#lang racket
(define printf
(lambda l
(for-each (lambda (a)
(display a)
(display " "))
l)
(newline)))
(define data '(2 3 (7 4 5 6) 8 (9) 2))
(define return
(lambda (v c)
(lambda (k)
(k v c))))
(define pipe
(lambda (m q)
(lambda (k)
(m (lambda (v c)
((q v c) k))))))
(define rember/evensXcount/evens_k_monad
(lambda (l)
(cond ((null? l)
(return '() 0))
((pair? (car l))
(pipe (rember/evensXcount/evens_k_monad (car l))
(lambda (v1 c1)
(pipe (rember/evensXcount/evens_k_monad (cdr l))
(lambda (v2 c2)
(return (cons v1 v2)
(+ c1 c2)))))))
((or (null? (car l)) (odd? (car l)))
(pipe (rember/evensXcount/evens_k_monad (cdr l))
(lambda (v c)
(return (cons (car l) v)
c))))
(else
(pipe (rember/evensXcount/evens_k_monad (cdr l))
(lambda (v c)
(return v (add1 c))))))))
((rember/evensXcount/evens_k_monad data)
(lambda (v c) (cons v c)))