1

Possible Duplicate:
Wrong IO actions order using putStr and getLine

I'm a haskell beginner.

I'm trying to make a program that shows a menu through terminal and ask user to introduce an option. Here is the code:

main :: IO ()
main = do
    putStrLn "0    <- quit"
    putStrLn "1    <- Hello"
    putStr "Choose an option: "
    c <- getChar
    case c of
        '0' -> return ()
        '1' -> putChar '\n' >> putStrLn "Hello World" >> main

When I use this module in the ghci interpreter everything works like it's suposed to do. But if i compile this with:

ghc hello.hs

and run it in the terminal, it doesn't display the line "Choose an option:" before ask for a char to be introduced. I think this may be caused because of haskell lazy nature and I don't know how to fix it.

Any ideas?

Community
  • 1
  • 1
carlos
  • 11
  • 1

1 Answers1

2

It's not due to laziness, just buffering. Use putStrLn instead of putStr and it will work.

See Wrong IO actions order using putStr and getLine

Community
  • 1
  • 1
ljedrz
  • 20,316
  • 4
  • 69
  • 97