-
Notifications
You must be signed in to change notification settings - Fork 0
/
monad-5.scm
40 lines (32 loc) · 952 Bytes
/
monad-5.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
;;; returns the list (a1+0^2 a2+1^2 0 a_i + i^2 ...), given a list (a1
;;; a2 a3 ...). returns 0 on each index multiple of 3. remove
;;; negative elements.
(load "libscm")
(define unit
(lambda (val)
(lambda (g)
(cons g val))))
(define >>=
(lambda (monadic sequel)
(lambda (l)
(let ((pair (monadic l)))
(let ((new/global (car pair))
(val (cdr pair)))
((sequel val)
new/global))))))
(define f+x2
(lambda (l)
(if (null? l)
(unit '())
(>>= (lambda (g)
(cons (+ g 1) (if (zero? (remainder g 3))
0
(* g g))))
(lambda (x2)
(>>= (f+x2 (cdr l))
(lambda (d)
(unit
(cons (+ x2 (car l)) d)))))))))
(test ((f+x2 '()) 0))
(test ((f+x2 '(0 0 0 0 0 0 0 0 0 0 0 0)) 0))
(test ((f+x2 '(1 2 3)) 0))