aboutsummaryrefslogtreecommitdiff
path: root/control/monad/state-minimal.scm
blob: 4fb40c24b82392930943aa53e7b096ea33a587f9 (plain)
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
(use-modules (ice-9 curried-definitions)
             (ice-9 match))

;;; This is a minimal implementation of the state monad.
;;; It is incompatible with the rest of my monad system,
;;; since everything either has type <pair> or <procedure>.
;;; But it should work as a nice base for the actual
;;; implementation.

;;; This implementation works, as far as I can tell, exactly
;;; like the Haskell version (of MonadState). But obviously
;;; without all the nice syntax.

(define ((get) st-list)
  "Sets the return value to the state value"
  (match st-list
    ((v st)
     (list st st))))

(define ((put v) st-list)
  "Sets the state value to v, sets the return value to ()"
  (list '() v))

(define ((bind st-proc proc) st-list)
  (let ((new-st-list (st-proc st-list)))
    (match new-st-list
      ((v _)
       ((proc v) new-st-list)))))

(define ((then st-proc-1 st-proc-b) st-list-a)
  (let ((st-list-b (st-proc-1 st-list-a)))
    (st-proc-b st-list-b)))

(define ((return v) st-list)
  "Sets the return value to v"
  (cons v (cdr st-list)))

(define (run-state st-proc init)
  "Exec state with init as starting state value"
  (st-proc (list init init)))