module Numeric.Probability.Monad where
import Control.Monad.HT ((<=<), )
import Control.Monad (liftM, )
import Prelude hiding (iterate, )
compose :: Monad m => [a -> m a] -> a -> m a
compose = foldl (flip (<=<)) return
iterate :: Monad m => Int -> (a -> m a) -> (a -> m a)
iterate n f = compose $ replicate n f
walk :: (Monad m) => Int -> (a -> m a) -> (a -> m [a])
walk n f =
let recourse 0 _ = return []
recourse m x = liftM (x:) (recourse (pred m) =<< f x)
in recourse n
doWhile :: Monad m => (a -> Bool) -> (a -> m a) -> (a -> m a)
doWhile p t =
let recourse x = t x >>= \l -> if p l then recourse l else return l
in recourse
while :: Monad m => (a -> Bool) -> (a -> m a) -> (a -> m a)
while p t =
let recourse x = if p x then t x >>= recourse else return x
in recourse
whileTrace :: Monad m => (a -> m Bool) -> m a -> m [a]
whileTrace p t =
do x <- t
b <- p x
liftM (x:) $
if b
then whileTrace p t
else return []