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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:56:26+00:00 2026-06-18T18:56:26+00:00

foldl :: (a -> b -> a) -> a -> [b] -> a foldl

  • 0
foldl :: (a -> b -> a) -> a -> [b] -> a
foldl step zero (x:xs) = foldl step (step zero x) xs
foldl _    zero []     = zero

I don’t quite understand why does (a -> b -> a) return a, also (a -> b -> a) -> a -> [b] -> a return a. I would think it should be like: (a -> b -> c) -> a -> [b] -> c. Can someone explain that to me based on the example below. Thanks!

foldl (+) 0 (1:2:3:[])
foldl (+) (0 + 1) (2:3:[])
foldl (+) ((0 + 1) + 2) (3:[])
foldl (+) (((0 + 1) + 2) + 3) ([])
foldl (+) (((0 + 1) + 2) + 3)
  • 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-18T18:56:27+00:00Added an answer on June 18, 2026 at 6:56 pm

    a represents the type of the accumulator value, and b represents the type of each element in the input. (a -> b -> a) is a function that takes an accumulator value, some item in the list, and returns a new accumulator value that can be passed onto the next step.

    The type of the initial value must be a so that the first invocation of the function can receive an accumulator value. The accumulator function must take an a and return an a so that an accumulator value can be passed to each step of the fold. The final value of the fold must necessarily be an a, because that is the type of the accumulator that will be returned by the final call the fold function.

    (a -> b -> c) -> a -> [b] -> c cannot represent a fold, because the folding function does not take a c. The input and output of the folding function must be the same type so the accumulator can be passed to the next fold step.

    Let’s see an example of what would happen if the fold function returned a c.

    f :: Integer -> Integer -> Bool   -- this is a valid (a -> b -> c)
    f acc n = (acc + n) > 0
    

    Pretend we’re using a dynamic language and we try to fold with f. What happens at runtime?

    foldl f 0 [1]     ~ (0 + 1) > 0            == True :: Bool
    
    foldl f 0 [1, 2]  ~ ((0 + 1) > 0) + 2) > 0 == error - no instance of (+) for Bool
                         \_________/   \
                              |         \
                             Bool      + Integer
    

    You can see that you can’t make a fold work if you try to accumulate with incompatible types.

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

Sidebar

Related Questions

I am using mvim and I would like to fold the class Experiment. How
I wanted to test foldl vs foldr. From what I've seen you should use
I would like to know if there is a built in function in python
It's feel like I'm stuck, my friends. Can somebody explain me pick equations from
To enable custom user preferences during publishing we would like to find out in
I often need to run reduce (also called foldl / foldr, depending on your
I understand how Map is easily parallelizable - each computer/CPU can just operate on
I understand and wrote a typical power set function in F# (similar to the
I have a function myLength = foldl (\ x _ -> x + 1)
Follow <Real World Haskell> , it is said foldl' are strict version of foldl

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.