Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top