In Haskell, I can easily define a recursive function that takes a value and returns a string:
Prelude> let countdown i = if (i > 0) then (show i) ++ countdown (i-1) else ""
Prelude> countdown 5
"54321"
I want to use the same design to read the available data from a file descriptor. In this particular case, I need to read the data in the same way as hGetContents, but without leaving the handle in a βhalf-closedβ state, so that I can interact with the stdin / stdout descriptors of the process opened with createProcess:
main = do
-- do work to get hin / hout handles for subprocess input / output
hPutStrLn hin "whats up?"
-- works
-- putStrLn =<< hGetContents hout
putStrLn =<< hGetLines hout
where
hGetLines h = do
readable <- hIsReadable h
if readable
then hGetLine h ++ hGetLines h
else []
Gives an error:
Couldn't match expected type `IO b0' with actual type `[a0]'
In the expression: hGetLine h : hGetLines h
I know that there are various libraries available to accomplish what I'm trying to accomplish, but I understand that my question really is how to do recursive I / O. TIA!
Scott