diff options
author | Hugo Hörnquist <hugo@hornquist.se> | 2018-11-13 00:36:09 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@hornquist.se> | 2018-11-13 00:38:38 +0100 |
commit | f1225201c9ded1078ef1f98fbf4969a8480d3b38 (patch) | |
tree | dfca9a64349927b9daf585939f3494b5d43742a6 /data/stack.scm | |
parent | Add define-stateful-method. (diff) | |
download | scheme-monad-f1225201c9ded1078ef1f98fbf4969a8480d3b38.tar.gz scheme-monad-f1225201c9ded1078ef1f98fbf4969a8480d3b38.tar.xz |
Add simple stateful stack implementation.
Diffstat (limited to 'data/stack.scm')
-rw-r--r-- | data/stack.scm | 21 |
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)))) |