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

Magnus Therning magnus at therning.org
Sun Nov 21 11:10:50 EST 2010


On 19/11/10 19:20, Brent Yorgey wrote:
> On Fri, Nov 19, 2010 at 03:37:10PM +0000, Magnus Therning wrote:
>> On Fri, Nov 19, 2010 at 15:31, Brent Yorgey <byorgey at seas.upenn.edu> wrote:
>>> On Fri, Nov 19, 2010 at 03:26:04PM +0000, Magnus Therning wrote:
>>>> On Fri, Nov 19, 2010 at 15:21, Brent Yorgey <byorgey at seas.upenn.edu> wrote:
>>>>> 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.
>>>>
>>>> I'm not sure I see any benefit of ': m a -> b -> m b' over 'm a -> m
>>>> ()'.  When would you want to use the former?
>>>
>>> >From the OP's message:
>>>
>>>  getLine >>= ignore something >>= putStrLn
>>>
>>> which executes 'something' for its side effect and passes the result
>>> of getLine through to putStrLn, without ever having to give a name to
>>> the result of getLine.  IIUC this was the whole point.
>>
>> IMNSHO, not such a strong argument for such a function though. :-)
>
> It depends what the "argument" is about.  I agree there is not a
> strong argument for including such a function in the standard
> libraries.  But it's a perfectly nice function to define and use in
> one's own code.  Haskell makes this sort of abstraction very cheap.

I can agree with that.

/M

-- 
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org           Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org/pipermail/beginners/attachments/20101121/8036ef8b/signature-0001.bin


More information about the Beginners mailing list