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

Brent Yorgey byorgey at seas.upenn.edu
Fri Nov 19 10:21:10 EST 2010


On Fri, Nov 19, 2010 at 07:56:02AM +0100, Tim Baumgartner wrote:
> Hi,
> 
> 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?

Nope, there isn't such a function, but I like it.  It reminds me of
(*>) and (<*) from Control.Applicative.  Note that you sometimes see
the name 'ignore' used for a slightly different function, namely

  ignore :: Monad m => m a -> m ()
  ignore m = m >> return ()

but yours is a bit more general.  Other names for your function might
be 'passThrough' or something like that.

-Brent


More information about the Beginners mailing list