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

The Archive Base Latest Questions

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

Hey guys, I need some help, how can I sum a list from a

  • 0

Hey guys, I need some help, how can I sum a list from a file using the sum builtin function?

example:
text.txt contains [1,2,3,4]

and i want to get this list from the file and sum these numbers with the sum builtin. Is it possible?

Thanks!

  • 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-22T01:42:57+00:00Added an answer on May 22, 2026 at 1:42 am

    Sure.

    main = do
        contents <- readFile "text.txt"
        print . sum . read $ contents
    

    This uses some standard functions from the Prelude:

    • readFile :: FilePath -> IO String reads the file into a string.

    • read :: Read a => String -> a converts from a String to any type in the Read typeclass. You might have to add a type annotation if the compiler couldn’t figure out which type you wanted. In this case the compiler can figure it out since we used sum on the result, so it infers that a must be [Integer]. (It actually infers Num a => [a], but this defaults to [Integer] due to type defaulting).

      read expects the same format generated by show on the same type.

    Note that I had to use do-notation to extract the String from IO String in order to apply read to it.

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

Sidebar

Related Questions

Hey guys! I need some help writing a code that creates an array in
Hey guys, I need to make a list of some functions that are listed
Hey guys need some more help I have 3 tables USERS, PROFILEINTERESTS and INTERESTS
Hey guys i need some help with logic in my app. I have an
Hey guys I need some help with this: I have two view controllers, let's
Hey Guys I would very much appreciate some help with the following. We're using
Hey guys, thanks for all the help that you can provide. I need a
Hey Guys, here is my code for this, the only help i get from
Hey guys, I'm writing a word wrap function to format console text in C++.
Hey, i need some help with rails, again ! Last it was about Authlogic..

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.