0
import System.IO
fileName = "data.txt"
main = do
  putStr "Enter Point :"
  information <- getLine
  let final = ['\n'] ++ information
  appendFile fileName final
  putStrLn "Done !"

Successful compilation but there is a runtime error : Program asks for input first then executes the putStr function.

hello
Enter Point :Done !

I think there should be an explanation of why Haskell isn't following the sequence (maybe it's because of its functional nature). Any explanation will be highly appreciated ! ( I am new to functional paradigm )

Azlan Khan
  • 11
  • 2

1 Answers1

1

It is following the sequence, it is simply buffering the `Enter Point :" part since the standard buffering policy is to wait until a next line is written to the output channel.

You can flush the standard output channel with:

import System.IO

fileName = "data.txt"
main = do
  putStr "Enter Point :"
  hFlush stdout  -- flush the output channel
  information <- getLine
  let final = ['\n'] ++ information
  appendFile fileName final
  putStrLn "Done !"
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555