import Data.Char
main = do
c <- getChar
if not $ isUpper c
then do putChar $ toUpper c
main
else putChar '\n'
Loading and executing in GHCi:
λ> :l foo.hs
Ok, modules loaded: Main.
λ> main
ñÑsSjJ44aAtTR
λ>
This consumes one character at time.
But in a terminal:
[~ %]> runhaskell foo.hs
utar,hkñm-Rjaer
UTAR,HKÑM-
[~ %]>
it consumes one line at time.
Why does it behave differently?
When you run a program in terminal it uses
LineBufferingby default, but inghciit is set toNoBuffering. You can read about it here. You will have to remove buffering fromstdinandstdoutto get the similar behavior.