[Haskell-beginners] Explanation of elem data type
Galaxy Being
borgauf at gmail.com
Wed Aug 4 00:36:20 UTC 2021
Thanks for the insights. So what then does the t a part mean? In the
simpler version t a is [a]. Is t some container? It's not meant to be a
function, is it?
On Tue, Aug 3, 2021 at 5:39 PM Francesco Ariis <fa-ml at ariis.it> wrote:
> 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
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
--
⨽
Lawrence Bottorff
Grand Marais, MN, USA
borgauf at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210803/2679ab2d/attachment.html>
More information about the Beginners
mailing list