How to use monadic expressions in Haskell correctly without analysis errors?

I am running GHC version 7.8.3 on Windows 7.

Well, these are not phrasal code snippets. I'm just trying not to be a noob here and actually compiling something in a way that vaguely resembles the structure of side effect languages.

I have the following code:

main = do { let x = [0..10]; print x } 

I found out here that the do keyword is fancy syntactic sugar for fancy monadic expressions. When I try to compile it, I get the following error:

 main.hs:4:1: parse error on input 'print' 

And I found out in this other question that tabs in Haskell are evil, so I tried to skip them:

 main = do { let x = [0..10]; print x } 

And I failed because the parsing error persists.

I also found out here that print is syntactic sugar for a fantastic equivalent:

 main = do { let x = [0..10]; putStrLn $ show x } 

But instead, I get this error:

 main.hs:4:9: parse error on input 'putStrLn' 

Trying to run into my despair, I tried to skip the let keyword after reading this answer :

 main = do { x = [0..10]; print x } 

And then I get:

 main.hs:4:1: parse error on input '=' 

And in my last futile attempt, I even tried to omit ';' eg:

 main = do { let x = [0..10] print x } 

And received:

 main.hs:4:1: parse error on input 'print' 

So,

How to use monadic expressions in Haskell correctly without analysis errors? Is there any hope?

+5
source share
3 answers
 main = do let x = [0..10] print x 

works for me

and therefore

 main = do { let x = [0..10] in print x } 

I think you are trying to mix different syntax options.

+4
source

It took me a while to see what was really going on here:

 main = do { let x = [0..10]; print x } 

The above looks as if we have a do with two statements, which is great. Of course, common practice does not use explicit brackets and semicolons when indentation implicitly inserts them. But they should not be sick ... why then the foregoing does not perform parsing?

The real problem is that let opens a new block! The let block has no curly braces, so the indent rule is applied. The block begins with the definition x = [0..10] . Then there is a semicolon that promises defines the following definition, for example:

 let x = [0..10] ; y = ... 

or even

 let x = [0..10] ; y = ... -- must be indented as the x above, or more indented 

However, after the semicolon we find print , which even with indentation is less than x . According to the indentation rule, this is equivalent to inserting curly braces, for example:

 main = do { let { x = [0..10]; } print x } 

but the above is not analyzed. The error message does not apply to implicitly inserted brackets (which would be very confusing!), But only to the next line (in this case, unfortunately, it is almost confusing).

The code can be fixed, for example, by providing explicit curly braces for let :

 main = do { let { x = [0..10] }; print x } 

Above, the indentation is completely irrelevant: you can add line breaks and / or spaces without affecting parsing (for example, Java, C, etc.). In addition, we can move the semicolon below:

 main = do { let x = [0..10] ; print x } 

The specified semicolon is on the next line and is less indented than x , implicitly pasting } , which closes the let block. Indentation matters here because let uses the indentation rule. If we add a semicolon more, we can cause the same parsing error that we discovered earlier.

Of course, the most idiomatic choice is to use the indentation rule for the entire code:

 main = do let x = [0..10] print x 
+15
source

I was going to say, without useful information, that

 main = do let x = [0..10] print x 

Worked for me, but now I read about in in braces.

As a small aspect, I found http://echo.rsmw.net/n00bfaq.html quite readable about identification / formatting.

+5
source

All Articles