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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:45:25+00:00 2026-06-05T00:45:25+00:00

In haskell, the initial value for the foldl operator is clearly mandatory Prelude> foldl

  • 0

In haskell, the initial value for the foldl operator is clearly mandatory

Prelude> foldl (+) 0 [1]
1
Prelude> foldl (+) 0 []
0
Prelude> :t foldl
(a -> b -> a) -> a -> [b] -> a

But in the reduce function (or functools.reduce), the initial value is optional

reduce(function, sequence[, initial]) -> value

The only time the initial value is required is when the sequence is empty. That is congruent with the behaviour in haskell. Does reduce in python assumes that if the sequence is of size 1; then it handles these corner cases internally ?

>> reduce(operator.sub, [1])
1
>> reduce(operator.mul, [1])
1
>> reduce(operator.add, [1])
1
  • 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-05T00:45:26+00:00Added an answer on June 5, 2026 at 12:45 am

    From the manual:

    If initializer is not given and iterable contains only one item, the
    first item is returned


    I also want to comment on this:

    The only time the initial value is required is when the sequence is
    empty

    This is not entirely true. In a fold operation you usually always assume that you have an earlier result to work with. If your fold operation is appending elements to a list the initial element can be the empty list, so you can add elements to that. More generally, the initial value is always needed when the return type of the function differs from the type of the elements in the list.

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

Sidebar

Related Questions

In Haskell I can easily define a recursive function which takes a value and
Haskell's type safety is second to none only to dependently-typed languages. But there is
In Haskell, when I need a quick worker function or helper value, I usually
In Haskell in 5 steps the factorial function is defined as follows: let fac
In Haskell, there is a function take n list which returns the first n
In Haskell function type ( -> ) is given, it's not an algebraic data
Is it possible in Haskell to apply the function arrow type constructor (->) to
So I'm working through some initial chapter exercise of Real World Haskell and I
In Haskell, why would you define a function with a type constraint: ghci> :t
I found this partially on http://www.haskell.org/haskellwiki/State_Monad but it is not clear to me how

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.