Thread: Haskell question--handling EOF in interactive program
hello, i'm new haskell, , try use process files (i.e., tasks similar might done in awk). specifically, i'd process text input file until reaches eof printing output @ appropriate times, , maintain state it.
here's stab @ program kind of thing:
the problem @ end of input file, prints error message:php code:--simple interactive program with state, takes
--user input string, appends it to current state
--string, and prints the state string
--state string grows as program is run
main :: io ()
main = helper("") --initial state is empty string
helper :: string -> io ()
helper initialstate = do
linein <- getline
putstrln(snd (transition (linein, initialstate)))
helper(fst (transition (linein, initialstate)))
--pure function to implement logic--translates
--input and current state to next state and
--output
transition :: (string, string) -> (string, string)
transition inpair = (nextstate, nextstate) where nextstate = (snd inpair) ++ (fst inpair)
i figure eliminate message checking eof, i'm not sure how this. know of simple way this?code:interact.hs: <stdin>: hgetline: end of file
in advance
iseof
php code:--simple interactive program with state, takes
--user input string, appends it to current state
--string, and prints the state string
--state string grows as program is run
import system.io
main :: io ()
main = helper("") --initial state is empty string
helper :: string -> io ()
helper initialstate = do end <- iseof
if end
then putstr ""
else do linein <- getline
putstrln(snd (transition (linein, initialstate)))
helper(fst (transition (linein, initialstate)))
--pure function to implement logic--translates
--input and current state to next state and
--output
transition :: (string, string) -> (string, string)
transition inpair = (nextstate, nextstate) where nextstate = (snd inpair) ++ (fst inpair)
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] Haskell question--handling EOF in interactive program
Ubuntu
Comments
Post a Comment