logoalt Hacker News

8notetoday at 6:39 PM2 repliesview on HN

fold kinda does too, for setting the first combined value that you are assembling, and thus on an empty list you end up with that wrong value, same as the for loop


Replies

bspammertoday at 7:21 PM

japgolly’s signature for reduce above is slightly wrong, it should be `[A] -> ((A,A) -> A) -> Maybe A`.

I.e. there is no initial value to pass in, but the result is an Optional to handle the empty iterator case. That’s how rust does it, for example:

https://doc.rust-lang.org/std/iter/trait.Iterator.html#metho...

mrkeentoday at 7:07 PM

No, the sum of the first ten natural numbers is always 55. It is not "initialised" to some other number beforehand.