aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ravanan/work/monads.scm17
1 files changed, 17 insertions, 0 deletions
diff --git a/ravanan/work/monads.scm b/ravanan/work/monads.scm
index 4370ef3..830e7ce 100644
--- a/ravanan/work/monads.scm
+++ b/ravanan/work/monads.scm
@@ -39,6 +39,7 @@
state-return
state-let*
state-begin
+ state-sequence
current-state
set-current-state
run-with-state))
@@ -71,6 +72,19 @@
(lambda _
body ...)))))
+(define (sequence monad-type mvalues)
+ "Convert a list of monadic @var{mvalues} in @var{monad-type} into a monadic list
+of values."
+ (let ((return (monad-return monad-type)))
+ (mlet* monad-type ((reverse-list
+ (fold (lambda (mvalue mresult)
+ (mlet* monad-type ((value mvalue)
+ (result mresult))
+ (return (cons value result))))
+ (return (list))
+ mvalues)))
+ (return (reverse reverse-list)))))
+
(define-immutable-record-type <maybe>
(maybe value valid?)
maybe?
@@ -226,6 +240,9 @@ maybe-monadic."
(mbegin %state-monad
body ...))
+(define state-sequence
+ (cut sequence %state-monad <>))
+
(define (current-state)
"Return the current state as a state-monadic value."
(lambda (state)