21

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
Elliot Cameron
  • 5,235
  • 2
  • 27
  • 34
RCIX
  • 38,647
  • 50
  • 150
  • 207
  • 2
    check your indentation. make sure all the lines in the block have the same indentation. tabs and spaces are not the same – newacct Jan 06 '10 at 23:24
  • 2
    If you're working with indent-sensitive languages like Haskell and Python, do make sure that your editor has sane tab settings. `:set et ts=8 sw=4 sta` or so in Vim. – ephemient Jan 07 '10 at 00:04

3 Answers3

29

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)

ADEpt
  • 5,504
  • 1
  • 25
  • 32
20

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.

Chuck
  • 234,037
  • 30
  • 302
  • 389
7

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

Hai
  • 4,764
  • 8
  • 29
  • 26