def sequence(monad_t, monad_list): """Evaluates each action in sequence from left to right and collects the results.""" def helper(monad, acc): "Helper for sequence." return monad >= (lambda x: (acc >= (lambda xs: (monad_t.return_m(xs + [x]))))) return func.foldr(helper, monad_t.return_m([]), list(reversed(monad_list)))
def sequence_(monad_t, monad_list): """Evaluates each action in sequence from left to right and dumps the results.""" return func.foldr(monad_t.then, monad_t.return_m(func.Unit()), monad_list)
def msum(monad_t, monad_list): "Generalized concatenation." return func.foldr(monad_t.mplus(), monad_t.mzero(), monad_list)