[Haskell-cafe] Monads as control structures?
Creighton Hogg
wchogg at lotus.hep.wisc.edu
Thu Oct 27 14:13:36 EDT 2005
On Thu, 27 Oct 2005, Creighton Hogg wrote:
> On Thu, 27 Oct 2005, Robert Dockins wrote:
>
> >
> > On Oct 27, 2005, at 11:54 AM, Creighton Hogg wrote:
> >
> > > Hi,
> > > so I'm a newbie getting used to Haskell. I'm writing some
> > > simple things like genetic algorithms in it for practice,
> > > and I keep coming across something that really bugs me:
> > > are there any standard libraries that allow you to
> > > do imperative style for or while loops using monads to keep
> > > track of state?
>
> > One way is to create a list of the actions you want to execute, and
> > then use one of the sequence family of functions.
> > The actions can share state with an IORef or STRef or whatever.
> > Another option is to use a fold with >>= to allow
> > actions to pass their results directly to the next action. This
> > works even in "stateless" monads like the list monad.
> >
> > Some examples using sequence:
> >
> >
> > forMonad :: Monad m => a -> (a -> Bool) -> (a -> a) -> (a -> m ()) ->
> > m ()
> > forMonad init cond inc f = sequence_ $ map f $ takeWhile cond $
> > iterate inc init
> >
> > xTimes :: Monad m => Int -> (Int -> m ()) -> m ()
> > xTimes x f = sequence_ $ map f [0..(x-1)]
> >
> > main = do { forMonad 0 (<10) (+1) (putStrLn . show); xTimes 10 (\_ ->
> > putStrLn "hi") }
>
> Thanks to all who answered, this gives me a lot to chew on.
> I especially like these examples, because it makes me
> realize that I've been thinking about monads and evaluation
> wrong.
> I think doing genetic programming in Haskell is going to be
> easier than I thought.
>
More information about the Haskell-Cafe
mailing list