[Haskell-beginners] Re: Ignoring the result of a monadic computation

Ertugrul Soeylemez es at ertes.de
Fri Nov 19 19:58:24 EST 2010


"Tim Baumgartner" <jongleur0815 at gmx.de> wrote:

> while learning about monads, I had something like
>
> do
>    line <- getLine
>    something
>    putStrLn line
>
> and I wondered if I could write it in one line, without naming of
> parameters.  I finally came up with
>
> getLine >>= ignore something >>= putStrLn
>
> using
> ignore :: Monad m => m a -> b -> m b
> ignore m a = m >> return a
>
> I'm satisfied with this solution but searching hoogle I didn't find a
> standard function for my ignore. Am I missing something?

Importing Control.Applicative, you can do this:

  getLine <* something >>= putStrLn

The (<*) function has the following signature:

  (<*) :: (Applicative f) => f a -> f b -> f a

It sequences the two computations in order, but returns the result of
the first.  This is mostly useful in applicative parsers, where you
often need to skip unimportant stuff like spacing and use an earlier
result.


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/




More information about the Beginners mailing list