aboutsummaryrefslogtreecommitdiff
path: root/data
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2018-11-13 00:36:09 +0100
committerHugo Hörnquist <hugo@hornquist.se>2018-11-13 00:38:38 +0100
commitf1225201c9ded1078ef1f98fbf4969a8480d3b38 (patch)
treedfca9a64349927b9daf585939f3494b5d43742a6 /data
parentAdd define-stateful-method. (diff)
downloadscheme-monad-f1225201c9ded1078ef1f98fbf4969a8480d3b38.tar.gz
scheme-monad-f1225201c9ded1078ef1f98fbf4969a8480d3b38.tar.xz
Add simple stateful stack implementation.
Diffstat (limited to 'data')
-rw-r--r--data/stack.scm21
1 files changed, 21 insertions, 0 deletions
diff --git a/data/stack.scm b/data/stack.scm
new file mode 100644
index 0000000..c28d648
--- /dev/null
+++ b/data/stack.scm
@@ -0,0 +1,21 @@
+(define-module (data stack)
+ #:use-module (control monad)
+ #:use-module (control monad state))
+
+;;; Simple stateful stack module for showing the state monad
+;;; in action. These functions assume that they are in a
+;;; (state list) monad. But dynamic types!
+
+(define (pop)
+ (do st <- (get)
+ let top = (car st)
+ (put (cdr st))
+ (return-state top)))
+
+(define (peek)
+ (do st <- (get)
+ (return-state (car st))))
+
+(define (push v)
+ (do st <- (get)
+ (put (cons v st))))