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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:30:28+00:00 2026-05-23T07:30:28+00:00

I’m learning Haskell now and I’m facing the following problem: I want to rewrite

  • 0

I’m learning Haskell now and I’m facing the following problem:

I want to rewrite the ++ function using foldl’ and foldr. I’ve done it with foldr:

myConcat xs ys = foldr (:) ys xs

I can’t do it using foldl’. I’ve read in RealWorldHaskell that foldr is useful for doing this kind of thing.
Ok, but can’t I also write an equivalant to ++ using foldl? Can someone show me how I can do this (if it can be done… The book doesn’t mention anything about it)…

Does Haskell’s type mechanism prevent me from doing this? Every time I tried I got a type error…

  • 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-23T07:30:28+00:00Added an answer on May 23, 2026 at 7:30 am

    I’m guessing the error which you get is from trying to simply switch foldr to foldl':

    myConcat xs ys = foldl' (:) ys xs
    

    which produces the error (using my Hugs REPL):

    ERROR - Type error in application
    *** Expression     : foldl' (:) xs ys
    *** Term           : (:)
    *** Type           : a -> [a] -> [a]
    *** Does not match : [a] -> a -> [a]
    

    Notice in the last two lines (the provided type and the expected type) that the positions of [a] and a are in the opposite positions. This means we need a function which is sort of like (:), but which takes its arguments in the opposite order.

    Haskell has a function which does this for us: the flip function. Basically, flip is equivalent to

    flip :: (a -> b -> c) -> (b -> a -> c)
    flip f y x = f x y
    

    That is, flip takes a binary function as an argument, and returns another binary function whose arguments are reversed (“flipped”) from the original. So while (:) has the type a -> [a] -> [a], we see that flip (:) has the type [a] -> a -> [a], making it a perfect candidate as a parameter to foldl'.

    Using flip, we now have this code:

    myConcat xs ys = foldl' (flip (:)) ys xs
    

    This result from the fact that foldl' has the type (a -> b -> c) -> a -> [b] -> c

    Running this with arguments [1..5] and [6..10], we get a result of [5,4,3,2,1,6,7,8,9,10], which is almost what we want. The only problem is that the first list turns out backwards in the result. Adding a simple call to reverse gives us our final definition of myConcat:

    myConcat xs ys = foldl' (flip (:)) ys (reverse xs)
    

    Looking over this process shows one of the nice things that often comes up when writing Haskell code: when you run into a problem, you can solve it one (small) step at a time. This is especially true when you already have one working implementation, and you are simply trying to write another. The big thing to notice is that if you change one part of the implementation (in this case, changing foldr to foldl'), then many of the other needed changes simply fall out of the type definitions. The few that remain are correctedness problems which can readily be found, either by running test cases, or by looking at the exact nature of the functions used.

    PS: any Haskell guys who can freshen up that last line of code, feel free to do so. While it’s not horrible, I don’t find it to be very pretty. Unfortunately, I’m not that good with Haskell yet.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want to construct a data frame in an Rcpp function, but when I
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.