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 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 right now I'm using jQuery and I have some global variables to hold
hey guys, i'm getting an exception on the following inner exception: {Value cannot be
Hey guys i wrote a quick test. I want delete to call deleteMe which
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also
Hey so what I want to do is snag the content for the first
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey, I've been developing an application in the windows console with Java, and want
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey peoples, I've been studying Java for a couple of weeks, and have decided

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.