[Haskell-beginners] Monad operators: multiple parameters

Christopher Howard christopher.howard at frigidcode.com
Tue Jun 7 18:42:51 CEST 2011


On 06/07/2011 04:54 AM, Mats Rauhala wrote:
> On 04:30 Tue 07 Jun     , Christopher Howard wrote:
>> In a reply to an earlier question, someone told me that "do" expressions
>> are simply syntactic sugar that expand to expressions with the >> and
>>>> = operators. I was trying to understand this (and the Monad class) better.
>>
>> I see in my own experiments that this...
>>
>> main = do ln1 <- getLine
>>           putStrLn ln1
>>
>> translates to this:
>>
>> main = getLine >>= \ln1 -> putStrLn ln1
>>
>> However, what does this translate to?:
>>
>> main = do ln1 <- getLine
>>           ln2 <- getLine
>>           putStrLn (ln1 ++ ln2)
> 
> It translates to:
> 
> main = getLine >>= \ln1 -> getLine >>= \ln2 -> putStrLn (ln1 ++ ln2)
> 
> 
> 
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

Thank you Fischer and Rauhala for your prompt and helpful responses.

One final point of clarification, on a related note: am I correct in
reasoning that...

main = do let x = expression
          /remainder/

is the equivalent of...

main = let x = expression in /expanded remainder/

so that, for example...

main = ln1 <- getline
       let ln2 = "suffix"
       putStrLn (ln1 ++ ln2)

would translate to...

main = let ln2 = "suffix" in
        getLine >>= \ln1 -> putStrLn (ln1 ++ ln2)

This above example works, but I want to be sure I haven't misunderstood
any important technical points.

-- 
frigidcode.com
theologia.indicium.us



More information about the Beginners mailing list