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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:16:12+00:00 2026-06-17T07:16:12+00:00

This is what I get in the console: ghci> sum $ takeWhile (<10000000) [1..]

  • 0

This is what I get in the console:

ghci> sum $ takeWhile (<10000000) [1..]
49999995000000
(11.96 secs, 2174569400 bytes)

That’s over 2GB! I would imagine that sum can discard whatever it has already summed. How would you write this?

  • 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-06-17T07:16:13+00:00Added an answer on June 17, 2026 at 7:16 am

    You’re creating ten million Integers, and a lot of list cells. Also, you’re running interpreted code, if you ran it through the compiler, that would reduce the allocation somewhat.

    The main problem is that the interpreter doesn’t optimise at all, so the sum uses the lazy variant that builds a huge thunk. sum discards the part of the list it has consumed just fine, but it replaces it with a thunk to compute the result afterward, so

    sum [1,2,3,4 ...]
    

    becomes

    (...((((0 + 1) + 2) + 3) + 4) + ...)
    

    afterward. That’s not the optimal substitution, since addition of Integers is strict.

    At the ghci prompt, you should write

    Prelude Data.List> foldl' (+) 0 $ takeWhile (< 10000000) [1 .. ]
    49999995000000
    (1.41 secs, 1443355832 bytes)
    

    to fix that. In a compiled (with optimisations, of course) programme, sum will work fine.

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

Sidebar

Related Questions

I get this error when trying ruby script/console Rails requires RubyGems >= . Please
I recevie 0 errors but I get this in the console: >Exception in thread
When using this user.meta.logins.$inc(); I get this error in the console: Mongoose MongooseNumber#$inc /
I can't find any solution to get this jquery events to work. $(window).one('click',function(){ console.log('hide
Say I have a class that looks like this (get/set omited): class InfoClass{ String
I have a custom cmdlet that can be called like this: Get-Info .\somefile.txt My
I'm trying to get console output from a program using this library uZpRunConsoleApp .
Mozilla Firefox error console is telling me over and over again that: missing ;
When I look in the console I get this message 2010-09-18 17:04:05.284 Wasted Time[8998:207]
I am using Velocity with Spring. But in Eclipse console I get this error.

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.