From a2450b97eab0052955d64ba8fc46ebc655766f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 12 Nov 2018 22:24:23 +0100 Subject: Add minimal state example. --- control/monad/state-minimal.scm | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 control/monad/state-minimal.scm 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 or . +;;; 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))) + -- cgit v1.2.3