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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:57:40+00:00 2026-05-14T01:57:40+00:00

What are the usual causes of Error C stack overflow in the Hugs Haskell

  • 0

What are the usual causes of “Error C stack overflow” in the Hugs Haskell implementation.

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

    This can come up if you are used to functional languages in which it is common to do tail-recursion factorization. Say you have a function:

    sum = go 0
        where
        go accum [] = accum
        go accum (x:xs) = go (accum+x) xs
    

    Which, incidentally, is the same as

    sum = foldl (+) 0
    

    If we evaluate the function we can see the problem:

    sum [1,2,3,4]
    go 0 [1,2,3,4]
    go (0+1) [2,3,4]
    go ((0+1)+2) [3,4]
    go (((0+1)+2)+3) [4]
    go ((((0+1)+2)+3)+4) []
    (((0+1)+2)+3)+4
    

    Now to evaluate that expression Haskell uses a stack:

    EXPR            | STACK
    (((0+1)+2)+3)+4 | 
    ((0+1)+2)+3     | +4
    (0+1)+2         | +3 +4
    (0+1)           | +2 +3 +4
    1               | +2 +3 +4
    3               | +3 +4
    6               | +4
    10              |
    

    And this is where an overflow can occur. If you evaluated sum [1..10^6], that stack would be a million entries deep.

    But note the pathology here. You recurse over a list only to build up a huge fscking expression (“thunk”), and then use a stack to evaluate it. We would rather evaluate it as we are recursing, by making the tail recursion strict:

    sum = go 0
        where
        go accum [] = accum
        go accum (x:xs) = accum `seq` go (accum+x) xs
    

    That says to evaluate accum before trying to evaluate the recursive call, so we get (this may take a some patience to understand):

    sum [1,2,3,4]
    go 0 [1,2,3,4]
    let accum = 0 in accum `seq` go (accum+1) [2,3,4]
    go (0+1) [2,3,4]
    let accum = 0+1 in accum `seq` go (accum+2) [3,4]
    go (1+2) [3,4]
    let accum = 1+2 in accum `seq` go (accum+3) [4]
    go (3+3) [4]
    let accum = 3+3 in accum `seq` go (accum+4) []
    go (6+4) []
    6+4
    10
    

    So as we are traversing the list, we are computing the sum so we don’t have to use a deep stack to evaluate the result. This modified tail recursion pattern is encapsulated in Data.List.foldl’, so:

    sum = foldl' (+) 0
    

    A good rule of thumb is to never use foldl, because it always builds up giant thunks. Use foldl’ instead. If the output type has lazy structure (eg. a list or a function), use foldr. But there is no silver bullet for avoiding a stack overflow in general, you just have to understand the evaluation behavior of your program. This can be hard sometimes.

    But, if I understand correctly, a stack overflow always comes from trying to evaluate a gigantic thunk, though. So look for places where those could be created, and force evaluation to happen earlier.

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

Sidebar

Related Questions

The usual method of URL-encoding a unicode character is to split it into 2
As usual, some background information first: Database A (Access database) - Holds a table
The usual way to resolve lnk involve using WShell.WshShortcut or IShellLink that way :
The usual alpha symbol for regular expressions \w in the .NET Framework matches alphanumeric
The usual way to assign color box functionality on a link is like this:
The usual pattern for a singleton class is something like static Foo &getInst() {
We have the usual web.xml for our web application which includes some jsp and
What are the usual methods to compare two polygons for similarity? Vertices are in
Any idea regarding the usual storage capacity of a magnetic swipe card ( like
I've got the issue of IE6 showing the secure and nonsecure items error on

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.