[Haskell-cafe] Pesky monads...

Matthew Cox matt at mattcox.ca
Sat May 19 15:28:16 EDT 2007


It occurred to me that the predicate will generally be a monadic function itself, so here's a
refined version:

 :: Monad m => (a -> m Bool) -> (a -> m a) -> a -> m a
untilM pred f x = do c <- pred x
                     if c then return x
                          else f x >>= untilM pred f

Think of a computation in the State monad, a predicate will probably want to consult that state.

Matthew Cox

----- Original Message -----
From: Matthew Cox
To:  <haskell-cafe at haskell.org>
Sent:  Sat, 19 May 2007 13:10:40 -0600
Subject: Re: [Haskell-cafe] Pesky monads...

You can define one:

untilM :: Monad m => (a -> Bool) -> (a -> m a) -> a -> m a
untilM pred f x | pred x = return x
                | otherwise = f x >>= untilM pred f

Matthew Cox

----- Original Message -----
From: Andrew Coppin
To:  haskell-cafe at haskell.org
Sent:  Sat, 19 May 2007 19:49:40 +0100
Subject: [Haskell-cafe] Pesky monads...

I've been getting some pretty weird complaints from the type checker. 
And I just figured out why! Grr...

Is there any function that does the same thing as "until", but in a monad?

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list