1

For some reason, Haskell on my machine does never return from any getLine call. For instance, I tried to run the following code straight from Learn You a Haskell for Great Good:

main = do  
    putStrLn "Hello, what's your name?"  
    name <- getLine  
    putStrLn ("Hey " ++ name ++ ", you rock!")  

When I run it, the first line is printed, and I see my input when I type a name, however when I press Enter the program just blocks there and never prints the final line.

How should I fix this?

edit: I am running it from the Sublime IDE, maybe that has something to do with it

sepp2k
  • 363,768
  • 54
  • 674
  • 675
misja111
  • 329
  • 2
  • 11
  • How are you running the program? From an IDE/editor (which one?) or from the command line (which terminal emulator, OS etc.)? And when you say it blocks, that means that the program keeps running (i.e. it's not that the window (if there is one) just closes or you return to the command prompt), it just produces no output, right? Does the cursor move to the next line when you hit enter or is it as if you had never even pressed enter? – sepp2k Feb 12 '17 at 14:25
  • Thanks for asking, I should have mentioned it. I am running it from an IDE: Sublime – misja111 Feb 12 '17 at 14:32

2 Answers2

6

After doing a quick search on how Sublime runs programs, I found a youtube video (edit: and this SO post) which says that Sublime's "run program" functionality can only show output and isn't capable of reading input.

So it looks like you'll have to run your program from the command line or from within GHCi using :main. The latter might be the most convenient as Sublime actually supports a GHCi tab, so you can still do everything from within Sublime.

Community
  • 1
  • 1
sepp2k
  • 363,768
  • 54
  • 674
  • 675
3

This seems to be a limitation in Sublime's Build command (assuming that this is what you're using).

Sublime executes the script using runhaskell, but apparently, it doesn't capture STDIN (which makes kind of sense - build results are usually read-only and not an interactive session).

Workaround: run your script from the command line with

runhaskell script.hs

and everything works as expected

Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107