Why is the execution of getChar in the terminal different from its execution in GHCi?

import Data.Char main = do c <- getChar if not $ isUpper c then do putChar $ toUpper c main else putChar '\n' 

Download and run in GHCi:

 λ> :l foo.hs Ok, modules loaded: Main. λ> main ñÑsSjJ44aAtTR λ> 

This time consumes one character.

But in the terminal:

 [~ %]> runhaskell foo.hs utar,hkñm-Rjaer UTAR,HKÑM- [~ %]> 

it consumes one row at a time.

Why does he behave differently?

+7
source share
1 answer

When the program starts, the terminal uses LineBuffering by default, and ghci NoBuffering . You can read about it here . You will need to remove buffering from stdin and stdout to get similar behavior.

 import Data.Char import System.IO main = do hSetBuffering stdin NoBuffering hSetBuffering stdout NoBuffering foo foo = do c <- getChar if not $ isUpper c then do putChar $ toUpper c foo else putChar '\n' 
+11
source

All Articles