2

I am new in Haskell and during writing a small code in haskell, I faced an error named

Last generator in do {...} must be an expression

I tried hard to remove this error but failed.

I visit the stack overflow for this error and I found but the answer was given too much large and complex so I can't understand.

It is the link where I found about this error

My code is

main = do
    putStrLn "What is your name?"
    name <- getLine
    putStrLn ("Name of customer is "++ name)

Anybody can help me so this error can be resolved and your help must be appreciated

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    The code you post works. Perhaps you mixed up spaces and tabs. What happens if you post your code in the question back into the file? – Willem Van Onsem May 01 '18 at 13:42
  • 3
    @WillemVanOnsem It works *now* because your edit modified the indentation... – Daniel Wagner May 01 '18 at 13:44
  • The spaces are same in my code as I posted but it creates the same error and can't work – Rahat Batool May 01 '18 at 13:44
  • @RahatBatool: ah, I had the idea that this was more SO that formatted it the wrong way. Anyway, you need to add at least one spaces to inline a `do` block. – Willem Van Onsem May 01 '18 at 13:46
  • 1
    @DanielWagner [The original unindented version works too](https://ideone.com/e65yYm), though I'm not exactly sure why. – sepp2k May 01 '18 at 13:48
  • @sepp2k The original version isn't unindented (click "source" next to the original to see the actual indentation -- bad on SO for screwing this up so badly on a code-specific site!). But the unindented version works because the [NondecreasingIndentation](https://prime.haskell.org/wiki/NondecreasingIndentation) Haskell Prime proposal was accepted into Haskell 2010. This also motivates the "for beginners" condition in my answer; without that modification to the standard, I would just say categorically to indent `do` blocks deeper. – Daniel Wagner May 01 '18 at 13:50
  • @WillemVanOnsem Thanks alot – Rahat Batool May 01 '18 at 13:53
  • It is working now. Next time I take care of indentation – Rahat Batool May 01 '18 at 13:54
  • always use explicit `{`, `;`, and `}`, and you will never have these problems. – Will Ness May 01 '18 at 14:31

1 Answers1

6

In your original question, your four lines had these indentations:

  1. four spaces
  2. four spaces
  3. one tab
  4. one tab

At a guess, the indentation of (1) was inserted by StackOverflow's "insert code" button, so your original source probably had this indentation:

  1. zero spaces
  2. four spaces
  3. one tab
  4. one tab

GHC treats tabs as expanding to an eight-space tabstop, but each of the lines in a do block must align with each other (and for beginners, should be indented deeper than the enclosing block). Therefore I recommend one of these two solutions:

  1. zero spaces
  2. one tab
  3. one tab
  4. one tab

OR

  1. zero spaces
  2. four spaces
  3. four spaces
  4. four spaces

The following would also work (and is the smallest edit to the original by most sensible metrics), but I strongly recommend against it.

  1. zero spaces
  2. eight spaces
  3. one tab
  4. one tab

See Why shouldn't I mix tabs and spaces? for further discussion of whitespace style.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
  • Oh dear, I've just had a horrible thought. There is a smaller edit still that fixes it -- just a single character insertion. But it's too awful to include. – Daniel Wagner May 02 '18 at 11:49