diff options
author | Arun Isaac | 2025-01-13 01:46:17 +0000 |
---|---|---|
committer | Arun Isaac | 2025-01-19 16:44:26 +0000 |
commit | b2cfe8a9d18d658fc54139721a42f500c9520e23 (patch) | |
tree | fcc11d04f8493fde59223c23156d4c1a09409be6 | |
parent | c02207dbfebe55e29469e998b8178085f593bb63 (diff) | |
download | ravanan-b2cfe8a9d18d658fc54139721a42f500c9520e23.tar.gz ravanan-b2cfe8a9d18d658fc54139721a42f500c9520e23.tar.lz ravanan-b2cfe8a9d18d658fc54139721a42f500c9520e23.zip |
monads: Add sequence.
* ravanan/work/monads.scm (sequence): New function.
(state-sequence): New public function.
-rw-r--r-- | ravanan/work/monads.scm | 17 |
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) |