Haskell Compiled IO-Action order and Flushing -
this question has answer here:
i'm encountering strange behaviour io, within compiled haskell code. here's what's going on:
-- myscript.hs main = putstr "enter name: " <- getline putstrln (a ++ " - that's nice name!") i run in ghci calling main , works 1 expect, first printing enter name: , doing whatever it's afterwards. however, when compile ghc (with , without --make), first prompts line, , then prints @ once, this:
$ ./myscript jimmy johnson enter name: jimmy johnson - that's nice name! to clarify, want occur in following sequence:
$ ./myfixedscript enter name: jimmy johnson jimmy johnson - that's nice name! could explain why happens is, , how sequence io way expect to.
note i've tried changing first line of do statement _ <- putstr "enter name: ", still doesn't work.
the io actions happening in correct order, problem lies in how input , output pipes work. string "enter name: " written output buffer putstr before getline, buffer hasn't been flushed. adding hflush stdout after putstr flush buffer.
import system.io -- myscript.hs main = putstr "enter name: " hflush stdout <- getline putstrln (a ++ " - that's nice name!")
Comments
Post a Comment