RCIX
RCIX

Reputation: 39427

Haskell -- "The last statement in a 'do' construct must be an expression"

Like it says in the title: What does The last statement in a 'do' construct must be an expression mean? I ended my do block with a putStrLn like it shows in several examples I've seen, and i get an error.

Code:

main = do args <- getArgs
           file <-readFile "TWL06.txt"
           putStrLn results

Upvotes: 22

Views: 23776

Answers (3)

Hai
Hai

Reputation: 4886

Incorrect indentation can lead to this error. Also, is good not to use tabs, only spaces.

Upvotes: 7

ADEpt
ADEpt

Reputation: 5542

Most of the time, it's because your code is mis-aligned and compiler assumes that your "do" block ended prematurely (or has extra code that dont really belong there)

Upvotes: 30

Chuck
Chuck

Reputation: 237010

Your last line isn't something like someVar <- putStrLn "hello", by any chance, is it? You'll get that error if you try to do a variable binding on the last line, because it's equivalent to putStrLn "Hello" >>= \someVar -> — it expects there to be an expression at the end.

Upvotes: 21

Related Questions