Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in getLine functionality with GHCi vs. runhaskell

I've just stumbled upon a thing I don't understand.

If I am using GHCi and use getLine I get the following

Prelude> a <- getLine
Test<Backspace>oo<CR> -- the Backspace action results in a '^?'
Prelude> a
"Test\DELoo"

If I write the same in a Haskell file

module Main where

main :: IO ()
main = do a <- getLine
          putStrLn a

and run the script with runhaskell and enter the same input, the backspace deletes the 't' as expected.

Why is there a difference?

like image 607
epsilonhalbe Avatar asked Jan 14 '14 13:01

epsilonhalbe


1 Answers

This is due to different buffering behaviour in GHCi and GHC. GHCi has stdin (standard input stream) using NoBuffering by default, and GHC-compiled binaries / runhaskell use LineBuffering by default. If you explicitly do

hSetBuffering stdin NoBuffering

in your Haskell program, you'll be able to reproduce the GHCi behaviour. If you do

hSetBuffering stdin LineBuffering

in GHCi, you'll have lots of unwanted side effects, but can reproduce the runhaskell behaviour.

like image 134
kosmikus Avatar answered Oct 14 '22 04:10

kosmikus