4 ms·
> to structures that can be reduced to a summary value one element at a time. This is the key part in my mind. Or elsewhere in the documentation: > Class of
by davorak 3y ago
> to structures that can be reduced to a summary value one element at a time.
This is the key part in my mind. Or elsewhere in the documentation:
> Class of data structures that can be folded to a summary value.
https://hackage.haskell.org/package/base-4.18.0.0/docs/Data-Foldable.html https://hackage.haskell.org/package/base-4.18.0.0/docs/Data-...
Foldable is the generalization, in the mathematical sense of the word, of some list functions, but it is not just the list functions. Generalizing something and applying to other structures sometimes results in unintuitive behavior, but is mathematically correct.
`Maybe` makes mathematical sense with foldable. A function with the type that `length` has for Foldable `Foldable f => f a -> Int` makes sense as well. The number of items in a list or a tree, or various other structures makes sense for 'data structures that can be folded to a summary value'.
The design decision I think is whether or not you call `Foldable f => f a -> Int` in Foldable `length` or something else. Since you have `length` for list structures already and `Foldable f => f a -> Int` is `length` for lists you can call it `length` instead of having two functions `length :: [a] -> Int` and lets say `foldCount :: Foldable f => f a -> Int` that both do the same things for lists.
I am pretty sure a discussion on this decision popped up, both in mailing lists and r/haskell, when they made the switch from `length :: [a] -> Int` to `length :: Foldable f => f a -> Int` in the haskell prelude. I did not follow it closely though both reduction in function count, pedagogical arguments and more came up.
edit - The proposal was the FTP proposal if you want to google those conversations.
Some links to start with
FAQ:
https://wiki.haskell.org/Foldable_Traversable_In_Prelude https://wiki.haskell.org/Foldable_Traversable_In_Prelude
random reddit conversation on r/haskell that popped up on a google search:
https://www.reddit.com/r/haskell/comments/3okick/foldable_for_nonhaskellers_haskells_controversial/ https://www.reddit.com/r/haskell/comments/3okick/foldable_fo...