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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:07:36+00:00 2026-05-16T04:07:36+00:00

I wanted to test foldl vs foldr. From what I’ve seen you should use

  • 0

I wanted to test foldl vs foldr. From what I’ve seen you should use foldl over foldr when ever you can due to tail reccursion optimization.

This makes sense. However, after running this test I am confused:

foldr (takes 0.057s when using time command):

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

main = putStrLn(show ( sum (foldr a [] [0.. 100000])))

foldl (takes 0.089s when using time command):

b::[b] -> b -> [b]
b xs = ( ++ xs). (\y->[y])

main = putStrLn(show ( sum (foldl b [] [0.. 100000])))

It’s clear that this example is trivial, but I am confused as to why foldr is beating foldl. Shouldn’t this be a clear case where foldl wins?

  • 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-16T04:07:37+00:00Added an answer on May 16, 2026 at 4:07 am

    Welcome to the world of lazy evaluation.

    When you think about it in terms of strict evaluation, foldl looks “good” and foldr looks “bad” because foldl is tail recursive, but foldr would have to build a tower in the stack so it can process the last item first.

    However, lazy evaluation turns the tables. Take, for example, the definition of the map function:

    map :: (a -> b) -> [a] -> [b]
    map _ []     = []
    map f (x:xs) = f x : map f xs
    

    This wouldn’t be too good if Haskell used strict evaluation, since it would have to compute the tail first, then prepend the item (for all items in the list). The only way to do it efficiently would be to build the elements in reverse, it seems.

    However, thanks to Haskell’s lazy evaluation, this map function is actually efficient. Lists in Haskell can be thought of as generators, and this map function generates its first item by applying f to the first item of the input list. When it needs a second item, it just does the same thing again (without using extra space).

    It turns out that map can be described in terms of foldr:

    map f xs = foldr (\x ys -> f x : ys) [] xs
    

    It’s hard to tell by looking at it, but lazy evaluation kicks in because foldr can give f its first argument right away:

    foldr f z []     = z
    foldr f z (x:xs) = f x (foldr f z xs)
    

    Because the f defined by map can return the first item of the result list using solely the first parameter, the fold can operate lazily in constant space.

    Now, lazy evaluation does bite back. For instance, try running sum [1..1000000]. It yields a stack overflow. Why should it? It should just evaluate from left to right, right?

    Let’s look at how Haskell evaluates it:

    foldl f z []     = z
    foldl f z (x:xs) = foldl f (f z x) xs
    
    sum = foldl (+) 0
    
    sum [1..1000000] = foldl (+) 0 [1..1000000]
                     = foldl (+) ((+) 0 1) [2..1000000]
                     = foldl (+) ((+) ((+) 0 1) 2) [3..1000000]
                     = foldl (+) ((+) ((+) ((+) 0 1) 2) 3) [4..1000000]
                       ...
                     = (+) ((+) ((+) (...) 999999) 1000000)
    

    Haskell is too lazy to perform the additions as it goes. Instead, it ends up with a tower of unevaluated thunks that have to be forced to get a number. The stack overflow occurs during this evaluation, since it has to recurse deeply to evaluate all the thunks.

    Fortunately, there is a special function in Data.List called foldl' that operates strictly. foldl' (+) 0 [1..1000000] will not stack overflow. (Note: I tried replacing foldl with foldl' in your test, but it actually made it run slower.)

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

Sidebar

Related Questions

I've wanted to test if with multiply processes I'm able to use more than
just wanted to gather different ideas and perspectives as to which layer should (and
simple example in Notepad++ using RegEx replace search for: anything replace with (wanted): \test
I wanted to test if a key exists in a dictionary before updating the
I wanted to test the following code (which works fine for a non-null list)
I wanted to test Mono AOT, so I wrote a simple console application with
I wanted to test the mapKit and wanted to make my own overlay to
I wanted to test my app on the device, but I dont want now
If i wanted to test an app with push notification service, does it have
Reading this question , I wanted to test if I could demonstrate the non-atomicity

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.