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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:37:40+00:00 2026-06-13T07:37:40+00:00

I’m not sure what the difference between these two pieces of code is (with

  • 0

I’m not sure what the difference between these two pieces of code is (with respect to x), but the first one completes:

$ foldr (\x y -> if x == 4 then x else x + y) 0 [1,2 .. ]
10

and the second one doesn’t (at least in GHCi):

$ foldr (\x (y, n) -> if x == 4 then (x, n) else (x + y, n + 1)) (0, 0) [1,2 .. ]
.......

What am I doing wrong that prevents the second example from completing when it hits x == 4, as in the first one?

I’ve tried adding bang-patterns to both the x and to the x == 4 (inside a let) but neither seems to make a difference.

  • 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-06-13T07:37:41+00:00Added an answer on June 13, 2026 at 7:37 am

    Your second example has two related problems.

    Recall that foldr produces a right-associative nesting, i.e., foldr f z [a, b, c] = f a (f b (f c z)). Thus, the second argument to the function you give foldr represents the final value of folding the entire remaining list. In the case of an infinite list, this is only possible if you either generate another lazy infinite data structure or, at some point, ignore the second parameter entirely, as your first example does.

    Your second example always uses the second item in the input tuple to compute the second item of the result tuple, so when applied to an infinite list you end up with an infinite regress. Specifying 0 as the “starting” value doesn’t help, because in foldr the starting value is placed at the end of the list, which an infinite list clearly doesn’t have–it seems you want to pass the value along unchanged, but that requires having a value to begin (or perhaps “end”) with!

    That much only causes the second item in the result to be non-terminating. The first item of the result is well-defined, or at least could be. The problem here is that pattern matching forces evaluation, which prevents any result from being produced until the entire fold is finished. This can be avoided in several ways, but simply using fst and snd is easy enough: \x yn -> if x == 4 then (x, snd yn) else (x + fst yn, snd yn + 1) should give you a result consisting of a tuple whose first item is what you expect (but whose second item is undefined, as explained above).

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was

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.