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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:31:44+00:00 2026-05-15T09:31:44+00:00

The code for the myAny function in this question uses foldr. It stops processing

  • 0

The code for the myAny function in this question uses foldr. It stops processing an infinite list when the predicate is satisfied.

I rewrote it using foldl:

myAny :: (a -> Bool) -> [a] -> Bool
myAny p list = foldl step False list
   where
      step acc item = p item || acc

(Note that the arguments to the step function are correctly reversed.)

However, it no longer stops processing infinite lists.

I attempted to trace the function’s execution as in Apocalisp’s answer:

myAny even [1..]
foldl step False [1..]
step (foldl step False [2..]) 1
even 1 || (foldl step False [2..])
False  || (foldl step False [2..])
foldl step False [2..]
step (foldl step False [3..]) 2
even 2 || (foldl step False [3..])
True   || (foldl step False [3..])
True

However, this is not the way the function behaves. How is this wrong?

  • 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-15T09:31:44+00:00Added an answer on May 15, 2026 at 9:31 am

    How folds differ seems to be a frequent source of confusion, so here’s a more general overview:

    Consider folding a list of n values [x1, x2, x3, x4 ... xn ] with some function f and seed z.

    foldl is:

    • Left associative: f ( ... (f (f (f (f z x1) x2) x3) x4) ...) xn
    • Tail recursive: It iterates through the list, producing the value afterwards
    • Lazy: Nothing is evaluated until the result is needed
    • Backwards: foldl (flip (:)) [] reverses a list.

    foldr is:

    • Right associative: f x1 (f x2 (f x3 (f x4 ... (f xn z) ... )))
    • Recursive into an argument: Each iteration applies f to the next value and the result of folding the rest of the list.
    • Lazy: Nothing is evaluated until the result is needed
    • Forwards: foldr (:) [] returns a list unchanged.

    There’s a slightly subtle point here that trips people up sometimes: Because foldl is backwards each application of f is added to the outside of the result; and because it is lazy, nothing is evaluated until the result is required. This means that to compute any part of the result, Haskell first iterates through the entire list constructing an expression of nested function applications, then evaluates the outermost function, evaluating its arguments as needed. If f always uses its first argument, this means Haskell has to recurse all the way down to the innermost term, then work backwards computing each application of f.

    This is obviously a far cry from the efficient tail-recursion most functional programmers know and love!

    In fact, even though foldl is technically tail-recursive, because the entire result expression is built before evaluating anything, foldl can cause a stack overflow!

    On the other hand, consider foldr. It’s also lazy, but because it runs forwards, each application of f is added to the inside of the result. So, to compute the result, Haskell constructs a single function application, the second argument of which is the rest of the folded list. If f is lazy in its second argument–a data constructor, for instance–the result will be incrementally lazy, with each step of the fold computed only when some part of the result that needs it is evaluated.

    So we can see why foldr sometimes works on infinite lists when foldl doesn’t: The former can lazily convert an infinite list into another lazy infinite data structure, whereas the latter must inspect the entire list to generate any part of the result. On the other hand, foldr with a function that needs both arguments immediately, such as (+), works (or rather, doesn’t work) much like foldl, building a huge expression before evaluating it.

    So the two important points to note are these:

    • foldr can transform one lazy recursive data structure into another.
    • Otherwise, lazy folds will crash with a stack overflow on large or infinite lists.

    You may have noticed that it sounds like foldr can do everything foldl can, plus more. This is true! In fact, foldl is nearly useless!

    But what if we want to produce a non-lazy result by folding over a large (but not infinite) list? For this, we want a strict fold, which the standard libraries thoughfully provide:

    foldl' is:

    • Left associative: f ( ... (f (f (f (f z x1) x2) x3) x4) ...) xn
    • Tail recursive: It iterates through the list, producing the value afterwards
    • Strict: Each function application is evaluated along the way
    • Backwards: foldl' (flip (:)) [] reverses a list.

    Because foldl' is strict, to compute the result Haskell will evaluate f at each step, instead of letting the left argument accumulate a huge, unevaluated expression. This gives us the usual, efficient tail recursion we want! In other words:

    • foldl' can fold large lists efficiently.
    • foldl' will hang in an infinite loop (not cause a stack overflow) on an infinite list.

    The Haskell wiki has a page discussing this, as well.

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

Sidebar

Ask A Question

Stats

  • Questions 434k
  • Answers 434k
  • 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 Perhaps something like this? exarkun@boson:/tmp/irc-server$ cat > passwd alice:secret bob:19820522… May 15, 2026 at 3:25 pm
  • Editorial Team
    Editorial Team added an answer Usually, you'd store it in a session variable, and when… May 15, 2026 at 3:25 pm
  • Editorial Team
    Editorial Team added an answer I wrote a simple class to do this. Maybe you'll… May 15, 2026 at 3:25 pm

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.