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

  • SEARCH
  • Home
  • 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 6722493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:27:13+00:00 2026-05-26T09:27:13+00:00

I have just recently started learning Haskell and I am having a lot of

  • 0

I have just recently started learning Haskell and I am having a lot of trouble trying to figure out how file reading works.

For example, I have a text file “test.txt” containing lines with numbers:

32 4
2 30
300 5

I want to read each line and then evaluate each word and add them.

Thus, I am trying to do something like this:

import System.IO
import Control.Monad

main = do
        let list = []
        handle <- openFile "test.txt" ReadMode
        contents <- hGetContents handle
        singlewords <- (words contents)
        list <- f singlewords
        print list
        hClose handle

f :: [String] -> [Int]
f = map read

I know this is completely wrong, but I don’t know how to use the syntax correctly at all.

Any help will be greatly appreciated as well as links to good tutorials that have examples and explanation of code except this one which I have read fully.

  • 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-26T09:27:14+00:00Added an answer on May 26, 2026 at 9:27 am

    Not a bad start! The only thing to remember is that pure function application should use let instead of the binding <-.

    import System.IO  
    import Control.Monad
    
    main = do  
            let list = []
            handle <- openFile "test.txt" ReadMode
            contents <- hGetContents handle
            let singlewords = words contents
                list = f singlewords
            print list
            hClose handle   
    
    f :: [String] -> [Int]
    f = map read
    

    This is the minimal change needed to get the thing to compile and run. Stylistically, I have a few comments:

    1. Binding list twice looks a bit shady. Note that this isn’t mutating the value list — it’s instead shadowing the old definition.
    2. Inline pure functions a lot more!
    3. When possible, using readFile is preferable to manually opening, reading, and closing a file.

    Implementing these changes gives something like this:

    main = do  
            contents <- readFile "test.txt"
            print . map readInt . words $ contents
    -- alternately, main = print . map readInt . words =<< readFile "test.txt"
    
    readInt :: String -> Int
    readInt = read
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just recently started carrying out unit testing for my software written in
I've just recently started learning HTML/CSS and I've been trying to teach myself sound
I just recently started trying out T4MVC and I like the idea of eliminating
I've just recently started using SQLAlchemy and am still having trouble wrapping my head
I have recently just started learning how to develop android applications. I am am
I just recently started learning Java. I have a question which is more about
I have just started C very recently and I have been asked to answer
I am just getting started with Silverlight and have recently added a Silverlight project
I have recently started learning Python and I have 2 questions relating to modules.
I have recently started learning Ruby, as my first programming language. I feel comfortable

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.