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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:05:48+00:00 2026-05-14T19:05:48+00:00

The canonical implementation of length :: [a] -> Int is: length [] = 0

  • 0

The canonical implementation of length :: [a] -> Int is:

length [] = 0
length (x:xs) = 1 + length xs

which is very beautiful but suffers from stack overflow as it uses linear space.

The tail-recursive version:

length xs = length' xs 0
  where length' [] n = n
        length' (x:xs) n = length xs (n + 1)

doesn’t suffer from this problem, but I don’t understand how this can run in constant space in a lazy language.

Isn’t the runtime accumulating numerous (n + 1) thunks as it moves through the list? Shouldn’t this function Haskell to consume O(n) space and lead to stack overflow?

(if it matters, I’m using GHC)

  • 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-14T19:05:49+00:00Added an answer on May 14, 2026 at 7:05 pm

    Yes, you’ve run into a common pitfall with accumulating parameters. The usual cure is to force strict evaluation on the accumulating parameter; for this purpose I like the strict application operator $!. If you don’t force strictness, GHC’s optimizer might decide it’s OK for this function to be strict, but it might not. Definitely it’s not a thing to rely on—sometimes you want an accumulating parameter to be evaluated lazily and O(N) space is just fine, thank you.

    How do I write a constant-space length function in Haskell?

    As noted above, use the strict application operator to force evaluation of the accumulating parameter:

    clength xs = length' xs 0
      where length' []     n = n
            length' (x:xs) n = length' xs $! (n + 1)
    

    The type of $! is (a -> b) -> a -> b, and it forces the evaluation of the a before applying the function.

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

Sidebar

Related Questions

The canonical JVM implementation from Sun applies some pretty sophisticated optimization to bytecode to
Implementing Equals() for reference types is harder than it seems. My current canonical implementation
What is the canonical way to access the MessageContext from a PayloadEndpoint? We are
I've tried to add the Canonical Partner repository, but I still can't find the
I'm using the following mod rewrites to ensure not only canonical URLs, but also
Is there a canonical or recommended pattern for implementing arithmetic operator overloading in C++
What is the canonical way of making your sprites respond to mouse clicks in
I had assumed that the canonical form for operator+, assuming the existence of an
I am studying for an exam and I am confused as to how canonical
For a number of reasons, the canonical source of some files I have can't

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.