-- | Monad helper functionsmoduleNumeric.Probability.MonadwhereimportControl.Monad.HT((<=<),)importControl.Monad(liftM,)importPreludehiding(iterate,)-- | composition of a list of monadic functionscompose::Monadm =>[a ->m a ]->a ->m a compose =foldl(flip(<=<))returniterate::Monadm =>Int->(a ->m a )->(a ->m a )iterate n f =compose $replicaten f -- | like 'iterate' but returns all intermediate values-- like Control.Monad.HT.iterateLimit, but counts differently.-- Is it actually useful this way?walk::(Monadm )=>Int->(a ->m a )->(a ->m [a ])walk n f =letrecourse 0_=return[]recoursem x =liftM(x :)(recourse (predm )=<<f x )inrecourse n {- | While loop with a posteriori check. Loops at least once. -}doWhile::Monadm =>(a ->Bool)->(a ->m a )->(a ->m a )doWhile p t =letrecourse x =t x >>=\l ->ifp l thenrecourse l elsereturnl inrecourse {- | While loop with a priori check. Can loop zero times. -}while::Monadm =>(a ->Bool)->(a ->m a )->(a ->m a )while p t =letrecourse x =ifp x thent x >>=recourse elsereturnx inrecourse whileTrace::Monadm =>(a ->m Bool)->m a ->m [a ]whileTrace p t =dox <-t b <-p x liftM(x :)$ifb thenwhileTrace p t elsereturn[]