aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2018-11-12 22:24:23 +0100
committerHugo Hörnquist <hugo@hornquist.se>2018-11-12 22:24:23 +0100
commita2450b97eab0052955d64ba8fc46ebc655766f98 (patch)
tree027623883ab2cfa4b6a2a99c4120840616ddcf3e
parentAdd >>, fix do to use it. (diff)
downloadscheme-monad-a2450b97eab0052955d64ba8fc46ebc655766f98.tar.gz
scheme-monad-a2450b97eab0052955d64ba8fc46ebc655766f98.tar.xz
Add minimal state example.
-rw-r--r--control/monad/state-minimal.scm41
1 files changed, 41 insertions, 0 deletions
diff --git a/control/monad/state-minimal.scm b/control/monad/state-minimal.scm
new file mode 100644
index 0000000..4fb40c2
--- /dev/null
+++ b/control/monad/state-minimal.scm
@@ -0,0 +1,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)))
+