[Haskell-beginners] Explanation of elem data type

Francesco Ariis fa-ml at ariis.it
Tue Aug 3 22:37:43 UTC 2021


Hello Lawrence,

Il 03 agosto 2021 alle 17:14 Galaxy Being ha scritto:
> According to this <http://zvon.org/other/haskell/Outputprelude/elem_f.html>
> elem is
> 
> Eq a => a -> [a] -> Bool
> 
> but according to my ghci :t it's this
> 
> elem :: (Foldable t, Eq a) => a -> t a -> Bool
> 
> I understand the first, but not the second, especially with the t. What is
> this saying extra, different from the first one?

Yup, some years ago there was a shift of some functions — after
a big discussion, as there were a few controversial changes — from
«working on lists only» to «working on all instances of some
class» (in this case: Foldable, alias «data structures that can
be folded») [1].

What is there for you? You gain the ability to use `elem` on
other structures than lists (e.g. Trees).
If you — like me — do not fancy reading signatures with too many
typeclasses, you can always use +d in ghci to default to concrete
types:

    λ> :t foldr
    foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
    λ> :t +d foldr
    foldr :: (a -> b -> b) -> b -> [a] -> b

[1] https://wiki.haskell.org/Foldable_Traversable_In_Prelude


More information about the Beginners mailing list