title: Recursive IO in Haskell
body: <p>In Haskell I can easily define a recursive function which takes a value and returns a string:</p>
  
  <pre><code>Prelude&gt; let countdown i = if (i &gt; 0) then (show i) ++ countdown (i-1) else ""
  Prelude&gt; countdown 5
  "54321"
  </code></pre>
  
  <p>I want to use the same kind of design to read available data from a file handle. In this particular case I need to read the data in the same fashion as hGetContents, but without leaving the handle in the "semi-closed" state, so that I can loop interaction with stdin/stdout handles of a process opened with createProcess:</p>
  
  
  
  <pre class="lang-hs prettyprint-override"><code>main = do
      -- do work to get hin / hout handles for subprocess input / output
  
      hPutStrLn hin "whats up?"
  
      -- works
      -- putStrLn =&lt;&lt; hGetContents hout
  
      putStrLn =&lt;&lt; hGetLines hout
  
      where
          hGetLines h = do
              readable &lt;- hIsReadable h
              if readable
                  then hGetLine h ++ hGetLines h
                  else []
  </code></pre>
  
  <p>Gives the error:</p>
  
  <pre><code>Couldn't match expected type `IO b0' with actual type `[a0]'
  In the expression: hGetLine h : hGetLines h
  </code></pre>
  
  <p>I know there are various libraries available for accomplishing what I'm trying to accomplish, but sice I'm learning my question is really how to perform recursive IO. TIA!</p>