From b2cfe8a9d18d658fc54139721a42f500c9520e23 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Mon, 13 Jan 2025 01:46:17 +0000 Subject: monads: Add sequence. * ravanan/work/monads.scm (sequence): New function. (state-sequence): New public function. --- ravanan/work/monads.scm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 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) -- cgit v1.2.3