-
Notifications
You must be signed in to change notification settings - Fork 0
/
little-monad-5.rkt
63 lines (56 loc) · 1.79 KB
/
little-monad-5.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
#lang racket
(define printf
(lambda l
(for-each (lambda (a)
(display a)
(display " "))
l)
(newline)))
(define data
'(2 3 (7 4 5 6) 8 (2) (9) 2 0))
(define return
(lambda (v)
(lambda (s)
(cons v s))))
(define pipe
(lambda (m q)
(lambda (s)
(let ((pair (m s)))
(let ((state (cdr pair))
(val (car pair)))
;; (printf "@@@@" state)
(let ((w (q val)))
(w state)))))))
(define rember/evensXcount/evens
(lambda (l)
;; (printf "--" (and (cons? l) (car l)))
(cond ((null? l)
(return '()))
((pair? (car l))
(pipe (rember/evensXcount/evens (car l))
(lambda (a)
(pipe (rember/evensXcount/evens (cdr l))
(lambda (d)
(return (cons a d)))))))
((or (null? (car l)) (odd? (car l)))
(pipe (lambda (s)
;; (printf "==" (car l) s)
(cons '_ (list (car s)
(if (null? (car l))
(cadr s)
0)
(max (caddr s) (cadr s)))))
(lambda _
(pipe (rember/evensXcount/evens (cdr l))
(lambda (d)
(return (cons (car l) d)))))))
(else
(pipe (lambda (s)
;; (printf "~~" (car l) s)
(cons '_ (list (add1 (car s))
(add1 (cadr s))
(caddr s))))
(lambda (v)
(rember/evensXcount/evens (cdr l))))))))
((rember/evensXcount/evens data) '(0 0 0 ()))
(~a ":" data)