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 996061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:49:11+00:00 2026-05-16T06:49:11+00:00

this may be a stupid question but i couldn’t find answer anywhere. I’m a

  • 0

this may be a stupid question but i couldn’t find answer anywhere. I’m a Haskell newbie and i’m having trouble with I/O.

I have this structure:

data SrcFile = SrcFile (IO Handle) String

srcFileHandle :: SrcFile -> IO Handle
srcFileHandle (SrcFile handle _) = handle

srcFileLine :: SrcFile -> String
srcFileLine (SrcFile _ string) = string

Now the problem is that i have no idea how to assign stdin/stderr/stdout into it, because the stdin etc are Handlers, no IO Handlers. And if i make the structure have Handle attributes insted of IO Handle, then i won’t be able to add any other file handles into it.

  • 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-16T06:49:11+00:00Added an answer on May 16, 2026 at 6:49 am

    Judging from your definition of SrcFile, it seems as though you may be trying to write a C program in Haskell. Language shapes the way we think, and the good news is Haskell is a much more powerful language!

    The excellent book Real World Haskell has a section on lazy I/O. Consider an excerpt:

    One novel way to approach I/O is the hGetContents function. hGetContents has the type Handle -> IO String. The String it returns represents all of the data in the file given by the Handle.

    In a strictly-evaluated language, using such a function is often a bad idea. It may be fine to read the entire contents of a 2KB file, but if you try to read the entire contents of a 500GB file, you are likely to crash due to lack of RAM to store all that data. In these languages, you would traditionally use mechanisms such as loops to process the file’s entire data.

    Here’s the radical part.

    But hGetContents is different. The String it returns is evaluated lazily. At the moment you call hGetContents, nothing is actually read. Data is only read from the Handle as the elements (characters) of the list are processed. As elements of the String are no longer used, Haskell’s garbage collector automatically frees that memory. All of this happens completely transparently to you. And since you have what looks like—and, really, is—a pure String, you can pass it to pure (non-IO) code.

    Further down is a section on readFile and writeFile that shows you how to forget about handles entirely.

    For example, say you want to grab all the import lines from a source file:

    module Main where
    
    import Control.Monad (liftM, mapM_)
    import Data.List (isPrefixOf)
    import System.Environment (getArgs, getProgName)
    import System.IO (hPutStrLn, stderr)
    
    main :: IO ()
    main = getArgs >>= go
      where go [path] = collectImports `liftM` readFile path >>= mapM_ putStrLn
            go _ = getProgName >>=
                   hPutStrLn stderr . ("Usage: " ++) . (++ " source-file")
    
    collectImports :: String -> [String]
    collectImports = filter ("import" `isPrefixOf`)
                   . takeWhile (\l -> null l
                                   || "module" `isPrefixOf` l
                                   || "import" `isPrefixOf` l)
                   . lines
    

    Even though the definition of main uses readFile, the program reads only as much of the named source-file as necessary, not the whole thing! There’s nothing magic going on: note that collectImports uses takeWhile to examine only those lines it needs to rather than, say, filter that would have to read all lines.

    When fed its own source, the program outputs

    import Control.Monad (liftM, mapM_)
    import Data.List (isPrefixOf)
    import System.Environment (getArgs, getProgName)
    import System.IO (hPutStrLn, stderr)
    

    So embrace laziness. Laziness is your friend! Enjoy the rest of the wonderful journey with Haskell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, this may seem like a stupid question (for Flash Developers) but I really
I'm new to ASP.NET MVC so this may be a stupid question. I have
This may seem like a stupid question, so here goes: Other than parsing the
This may seem like a daft question, but i was wondering about how to
This may be a no-brainer for the WPF cognoscenti, but I'd like to know
This may seem like a programming 101 question and I had thought I knew
This may be simple one, but 5 mins of Googling didn't give me the
This may be a simple fix - but I'm trying to sum together all
This may be a matter of style, but there's a bit of a divide
This may sound strange but sometimes when your ASP.NET webapp isn't working and you

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.