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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:05:55+00:00 2026-05-15T04:05:55+00:00

I’m wondering why Prelude> head $ reverse $ [1..10000000] ++ [99] 99 does not

  • 0

I’m wondering why

Prelude> head $ reverse $ [1..10000000] ++ [99]
99

does not lead to a stack overflow error. The ++ in the prelude seems straight forward and non-tail-recursive:

(++) :: [a] -> [a] -> [a]
(++) []     ys = ys
(++) (x:xs) ys = x : xs ++ ys

EDIT: Initially, I thought the issue has something to do with the way ++ is defined in the prelude, especially with the rewriting rules, hence the question continued as below. The discussion showed me that this is not the case. I think now that some lazy evaluation effect causes the code to run without a stack overflow, but I don’t quite figure how.

So just with this, it should run into a stack overflow, right? So I figure it probably has something to do with the ghc magic that follows the definition of ++:

{-# RULES
“++” [~1] forall xs ys. xs ++ ys = augment (\c n -> foldr c n xs) ys
#-}

*Is that what helps avoiding the stack overflow? Could someone provide some hint for what’s going on in this piece of code?**

  • 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-15T04:05:56+00:00Added an answer on May 15, 2026 at 4:05 am

    This doesn’t stack overflow – even in the interpreter, where there are no optimizations and no rewrite rules – because it doesn’t use the stack.

    Look at the definition of (++), for example,:

    (++) :: [a] -> [a] -> [a]
    (++) []     ys = ys
    (++) (x:xs) ys = x : xs ++ ys
    

    The key thing is x : (xs ++ ys) — that is, it is recursion guarded by the (:) “cons” constructor. Because Haskell is lazy, it allocates a thunk for the cons operation, and the recursive call goes onto this (heap-allocated) thunk. So your stack allocation is now heap allocation, which can expand quite a bit. So all this does is walk the big list, allocating new cons objects on the heap to replace the ones it is traversing. Easy!

    “reverse” is a bit different:

    reverse l =  rev l []
      where
        rev []     a = a
        rev (x:xs) a = rev xs (x:a)
    

    That is a tail recursive, accumulator-style function, so again, it will allocate on the heap.

    So you see, the functions rely on using cons cells on the heap, instead of on the stack, hence no stack overflow.

    To really nail this, look at the runtime stats from the GC vm:

    $ time ./B +RTS -s
    99
    
             833 MB total memory in use (13 MB lost due to fragmentation)
             Generation 0:  3054 collections,     0 parallel,  0.99s,  1.00s elapsed
             %GC time      82.2%  (85.8% elapsed)
    

    There’s your big list — it is allocated on the heap, and we spend 80% of the time cleaning up cons nodes that are created by (++).

    Lesson: you can often trade stack for heap.

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

Sidebar

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.