-
Notifications
You must be signed in to change notification settings - Fork 0
/
little-monad-3.rkt
55 lines (43 loc) · 1.05 KB
/
little-monad-3.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
#lang racket
(define printf
(lambda l
(for-each (lambda (a) (display a) (display " "))
l)
(newline)))
(define even-len?
(lambda (ls)
(cond ((null? ls) #t)
(else (not (even-len? (cdr ls)))))))
(even-len? '(1 2 3 4))
(even-len? '(1 2 3 4 5))
'---
(define even-len/?
(lambda (ls s)
(cond ((null? ls) s)
(else (even-len/? (cdr ls) (not s))))))
(even-len/? '(1 2 3 4) #t)
(even-len/? '(1 2 3 4 5) #t)
'---
(define even-len?__state
(lambda (ls)
(if (null? ls)
(unit-state '_)
(pipe-state (lambda (s)
(cons '__ (not s)))
(lambda _
(even-len?__state (cdr ls)))))))
(define unit-state
(lambda (v)
(lambda (s)
(cons v s))))
(define pipe-state
(lambda (m w)
(lambda (s)
(let ((p (m s)))
(let ((val (car p))
(state (cdr p)))
(let ((q (w val)))
(q state)))))))
(even-len?__state '(1 2 3 4 5))
((even-len?__state '(1 2 3 4 5)) #t)
((even-len?__state '(1 2 3 4)) #t)