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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:42:13+00:00 2026-05-11T18:42:13+00:00

I was doing the exercises from YAHT’s Recursive Datatype section, and found writing the

  • 0

I was doing the exercises from YAHT’s Recursive Datatype section, and found writing the listFoldr function a bit challenging (mainly because I didn’t really understand the difference between foldl and foldr at first). When I finally realized exactly how the foldr function worked, I decided that a simple swap of function arguments would be all that’d be needed to change my listFoldl function to a listFoldr function:

listFoldl f i [] = i
listFoldl f i (x:xs) = listFoldl f (f i x) xs

listFoldr f i [] = i
listFoldr f i (x:xs) = listFoldr f (f x i) xs

This appears to work (I did more tests than this):

Main> foldr (-) 4 [1, 2, 3]
-2
Main> listFoldr (-) 4 [1, 2, 3]
-2

But the solution given for the exercise is much different than mine. Their listFoldl is exactly the same as mine, but look at their listFoldr:

listFoldr f i [] = i
listFoldr f i (x:xs) = f x (listFoldr f i xs)

Which solution is better, mine or theirs? Is one of them incorrect? (In my tests, they both end up with the exact same result…)

  • 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-11T18:42:13+00:00Added an answer on May 11, 2026 at 6:42 pm

    I think you are processing the elements in the ‘opposite order’, and so yours is not right.

    You should be able to demonstrate this with an example where ‘order matters’. For example, something like

    listfoldr f "" ["a", "b", "c"]
    

    where ‘f’ is a function along the lines of

    f s1 s2 = "now processing f(" @ s1 @ "," @ s2 @ ")\n"
    

    where ‘@’ is a string-append operator (I forget what it is in Haskell). The point is just to ‘instrument’ the function so you can see what order it is getting called with the various args.

    (Note that this didn’t show up in your example because the math “4-1-2-3” yields the same answer as “4-3-2-1”.)

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

Sidebar

Ask A Question

Stats

  • Questions 163k
  • Answers 163k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Figured out why. I had generated ssh keys and copied… May 12, 2026 at 12:13 pm
  • Editorial Team
    Editorial Team added an answer The simplest way is to do something like this: $.ajax({… May 12, 2026 at 12:13 pm
  • Editorial Team
    Editorial Team added an answer According to your code, you are creating a new dataset… May 12, 2026 at 12:13 pm

Related Questions

I was doing the exercises from YAHT's Recursive Datatype section, and found writing the
I'm doing the exercises in Stroustrup's new book Programming Principles and Practice Using C++
I'm working through Real World Haskell , and at the moment doing the exercises
I was doing an exercise for university where I had to return a value
I've been tinkering with small functions on my own time, trying to find ways

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.