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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:21:22+00:00 2026-06-10T14:21:22+00:00

By the task we’ve had to implement foldl by foldr. By comparing both function

  • 0

By the task we’ve had to implement foldl by foldr. By comparing both function signatures and foldl implementation I came with the following solution:

myFoldl :: (a -> b -> a) -> a -> [b] -> a
myFoldl _ acc [] = acc
myFoldl fn acc (x:xs) = foldr fn' (fn' x acc) xs
    where
    fn' = flip fn

Just flip function arguments to satisfy foldr expected types and mimic foldl definition by recursively applying passed function.
It was a surprise as my teacher rated this answer with zero points.

I even checked this definition stacks its intermediate results in the same way as the standard foldl:

 > myFoldl (\a elm -> concat ["(",a,"+",elm,")"]) "" (map show [1..10])
 > "((((((((((+1)+10)+9)+8)+7)+6)+5)+4)+3)+2)"
 > foldl (\a elm -> concat ["(",a,"+",elm,")"]) "" (map show [1..10])
 > "((((((((((+1)+10)+9)+8)+7)+6)+5)+4)+3)+2)"

The correct answer was the following defintion:

myFoldl :: (a -> b -> a) -> a -> [b] -> a    
myFoldl f z xs = foldr step id xs z
    where step x g a = g (f a x)

Just asking why is my previous definition incorrect ?

  • 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-10T14:21:24+00:00Added an answer on June 10, 2026 at 2:21 pm

    Essentially, your fold goes in the wrong order. I think you didn’t copy your output from foldl correctly; I get the following:

    *Main> myFoldl (\ a elem -> concat ["(", a, "+", elem, ")"]) "" (map show [1..10])
    "((((((((((+1)+10)+9)+8)+7)+6)+5)+4)+3)+2)"
    *Main> foldl (\ a elem -> concat ["(", a, "+", elem, ")"]) "" (map show [1..10])
    "((((((((((+1)+2)+3)+4)+5)+6)+7)+8)+9)+10)"
    

    so what happens is that your implementation gets the first element–the base case–correct but then uses foldr for the rest which results in everything else being processed backwards.

    There are some nice pictures of the different orders the folds work in on the Haskell wiki:

    foldr visualization

    foldl visualization

    This shows how foldr (:) [] should be the identity for lists and foldl (flip (:)) [] should reverse a list. In your case, all it does is put the first element at the end but leaves everything else in the same order. Here is exactly what I mean:

    *Main> foldl (flip (:)) [] [1..10]
    [10,9,8,7,6,5,4,3,2,1]
    *Main> myFoldl (flip (:)) [] [1..10]
    [2,3,4,5,6,7,8,9,10,1]
    

    This brings us to a deeper and far more important point–even in Haskell, just because the types line up does not mean your code works. The Haskell type system is not omnipotent and there are often many–even an infinite number of–functions that satisfy any given type. As a degenerate example, even the following definition of myFoldl type-checks:

    myFoldl :: (a -> b -> a) -> a -> [b] -> a
    myFoldl _ acc _ = acc
    

    So you have to think about exactly what your function is doing even if the types match. Thinking about things like folds might be confusing for a while, but you’ll get used to it.

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

Sidebar

Related Questions

My task is to re-implement this function divn :: Integer -> [Integer] -> [Integer]
Task is following: how to create selected picture mosaic from large number of images.
Task Manager says that IE is using over 500MB of private working set. Both
my task is to make an e.g. of MVP implementation. I want to have
Task : Take two text files and output 100% matches and 75% matches. Solution
Task: implement paging of database records suitable for different RDBMS. Method should work for
The task is to implement a Java Card application (Applet) with biomentric (fingerprint)-based security.
Simple task. I'm using the websockets server implementation by jetty, and I have to
Task: To provide facility to upgrade the system remotely or add new features. What
Task at hand — I have three versions of some code, developed by different

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.