[Haskell-cafe] Textbook example of instance Foldable ((,) a)

Sebastiaan Joosten sjcjoosten+haskell at gmail.com
Mon Nov 23 15:30:51 UTC 2020


length is part of the Foldable class:

length = foldl'
<https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.Foldable.html#foldl%27>
(\c
<https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.Foldable.html#local-6989586621679508859>
_ -> c
<https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.Foldable.html#local-6989586621679508859>
+
<https://hackage.haskell.org/package/base-4.14.0.0/docs/src/GHC.Num.html#%2B>
1) 0
It is probably defined this way s.t. it satisfies:
length = length . toList
Not the actual definition, but you can read toList as:
toList = foldr (:) []

So as soon as one defines foldr for pairs:

foldr <https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.Foldable.html#local-6989586621679508613>
f <https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.Foldable.html#local-6989586621679508612>
z <https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.Foldable.html#local-6989586621679508611>
(_, y <https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.Foldable.html#local-6989586621679508610>)
= f <https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.Foldable.html#local-6989586621679508612>
y <https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.Foldable.html#local-6989586621679508610>
z <https://hackage.haskell.org/package/base-4.14.0.0/docs/src/Data.Foldable.html#local-6989586621679508611>

We get:
toList (a,b) = [b]

Using the 'natural' property: length = length . toList
We obtain: length (a,b) = length [b] = 1


On Mon, Nov 23, 2020 at 10:22 AM Manuel Schneckenreither <
manuel.schnecki at gmail.com> wrote:

>     >>>>> "HT" == Henning Thielemann <lemming at henning-thielemann.de>
> writes:
>
>     HT> "length (a,b) == 1" is only a consequence of defining instance
>     HT> Foldable for pairs. It was not the argument to implement that
>     HT> instance, at all.
>
> What was the argument to use 1, not 2 for instance?
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20201123/d0080bc6/attachment.html>


More information about the Haskell-Cafe mailing list