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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:40:46+00:00 2026-05-12T08:40:46+00:00

Running the following program will print space overflow: current size 8388608 bytes. I have

  • 0

Running the following program will print “space overflow: current size 8388608 bytes.” I have read this and this, but still don’t know how to resolve my problem. I am using foldr, shouldn’t it be guaranteed to be “tail recursive”?

I feel great about Haskell so far until I know I should prevent “space overflow” when using the powerful recursion. 🙂

module Main where
import Data.List

value a  b = 
  let l = length $ takeWhile (isPrime) $ map (\n->n^2 + a * n + b) [0..]
  in (l, a ,b)

euler27 = let tuple_list = [value a b | a <-[-999..999] , b <- [-999..999]]
      in foldr (\(n,a,b) (max,v) -> if n > max then (n , a * b) else (max ,v) ) (0,0) tuple_list
main = print euler27

EDIT: remove the definiton of isPrime for simplicity

  • 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-12T08:40:46+00:00Added an answer on May 12, 2026 at 8:40 am

    As pierr answered, you should use foldl'. For more details:

    • foldl' calculates its “left-side” before giving it to your fold step.
    • foldr gives your fold step a “thunk” for the right-side value. This “thunk” will be calculated when needed.

    Let’s make a sum with foldr and see how it evaluates:

    foldr (+) 0 [1..3]
    1 + foldr (+) 0 [2..3]
    1 + 2 + foldr (+) 0 [3]
    1 + 2 + 3 + foldl (+) 0 [] -- this is a big thunk..
    1 + 2 + 3 + 0
    1 + 2 + 3
    1 + 5
    6
    

    And with foldl': (tag omitted in code because SO doesn’t display it nicely)

    foldl (+) 0 [1..3]
    -- seq is a "strictness hint".
    -- here it means that x is calculated before the foldl
    x `seq` foldl (+) x [2..3] where x = 0+1
    foldl (+) 1 [2..3]
    x `seq` foldl (+) x [3] where x = 1+2
    foldl (+) 3 [3]
    x `seq` foldl (+) x [] where x = 3+3
    foldl (+) 6 []
    6
    

    In good uses for foldr, which don’t leak. the “step” must either:

    • Return a result that doesn’t depend on the “right-side”, ignoring it or containing it in a lazy structure
    • Return the right-side as is

    Examples of good foldr usage:

    -- in map, the step returns the structure head
    -- without evaluating the "right-side"
    map f = foldr ((:) . f) []
    
    filter f =
      foldr step []
      where
        step x rest
          | f x = x : rest -- returns structure head
          | otherwise = rest -- returns right-side as is
    
    any f =
      foldr step False
      where
        -- can use "step x rest = f x || rest". it is the same.
        -- version below used for verbosity
        step x rest
          | f x = True -- ignore right-side
          | otherwise = rest -- returns right-side as is
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 180k
  • Answers 180k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For Google Load you have to use: google.setOnLoadCallback(function() { $(document).ready(function()… May 12, 2026 at 4:02 pm
  • Editorial Team
    Editorial Team added an answer Add an import statement at the top of your class… May 12, 2026 at 4:02 pm
  • Editorial Team
    Editorial Team added an answer ISpMMSysAudio::SetLineId actually specifies the mixer index, not the line ID.… May 12, 2026 at 4:02 pm

Related Questions

A book as the following example and explains that you won't see the line
So it's the third week of my life in C and I've been given
I need to run a task every 10 mins from morning 9 AM to
I am looking for a tool like ltrace or strace that can trace locally

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.