diff options
Diffstat (limited to 'monad/stack.scm')
-rw-r--r-- | monad/stack.scm | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/monad/stack.scm b/monad/stack.scm new file mode 100644 index 0000000..8d25303 --- /dev/null +++ b/monad/stack.scm @@ -0,0 +1,24 @@ +(define-module (monad stack) + #:export (pop peek push) + #:use-module (monad) + #:use-module (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! + +;;; TODO test these for empty stack + +(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)))) |