Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5967417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:56:29+00:00 2026-05-22T19:56:29+00:00

In Haskell I can easily define a recursive function which takes a value and

  • 0

In Haskell I can easily define a recursive function which 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 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:

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 the error:

Couldn't match expected type `IO b0' with actual type `[a0]'
In the expression: hGetLine h : hGetLines h

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:56:29+00:00Added an answer on May 22, 2026 at 7:56 pm

    Naive solution, strict and O(n) stack

    You still have to use the do-notation, which would lead to this:

    import System.IO
    import System.IO.Unsafe (unsafeInterleaveIO)
    
    -- Too strict!
    hGetLines :: Handle -> IO [String]
    hGetLines h = do
        readable <- hIsReadable h
        if readable
            then do
                x  <- hGetLine h
                xs <- hGetLines h
                return (x:xs)
            else return []
    

    But see my comment, this version of hGetLines is too strict!

    Lazy, streaming version

    It won’t return your list, until it has all the input. You need something a bit lazier. For this, we have unsafeInterleaveIO,

    -- Just right
    hGetLines' :: Handle -> IO [String]
    hGetLines' h = unsafeInterleaveIO $ do
        readable <- hIsReadable h
        if readable
            then do
                x  <- hGetLine h
                xs <- hGetLines' h
                return (x:xs)
            else return []
    

    Now you can start streaming results line-by-line to your consumer code:

    *Main> hGetLines' stdin
    123
    ["123"345
    ,"345"321
    ,"321"^D^CInterrupted.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know a Haskell module name, but I can't figure out in what package
When I open a file for reading in Haskell, I've found that I can't
I'm currently learning Haskell, Which language (F# or Haskell) do you prefer for programming
How can one create a Haskell/C++ style constructor in C? Also, once I have
Haskell is givinig me a headache today. I want to handle an exception. When
In Haskell, is there a way to restrict a monad M a so that
From the haskell report: The quot, rem, div, and mod class methods satisfy these
In reading Haskell-related stuff I sometimes come across the expression tying the knot, I
I was reading a research paper about Haskell and how HList is implemented and
I'm currently working on project with Haskell, and have found myself some trouble. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.