[Haskell-cafe] Determining the length of a Foldable Applicative.

David Kraeutmann kane at kane.cx
Fri Oct 30 21:56:29 UTC 2015


Your types don't match here, but you have the right idea.

'sum' won't be enough. Take a look at the definition of sum:

> sum :: (Foldable t, Num a) => t a -> a
> sum = getSum . foldMap Sum

That won't work, since that requires '(Num a)'.

We just want to count 1 up whenever we map an element.
Try doing something with const and Sum.

(Forgot to reply to list)

On 10/30/2015 9:03 PM, David Banas wrote:
> Hi all,
> 
> I thought I had a simple way to determine the “length" (i.e. - number of elements in) of a Foldable Applicative container:
> 
>     import Prelude hiding (sum)
>     import Data.Foldable (Foldable(..), sum)
>     import Control.Applicative
> 
>     -- Calculate the "length" (i.e. - number of elements in) an Applicative container.
>     app_len :: (Applicative f, Foldable f) => f a -> Int
>     app_len = sum $ pure 1
> 
> but I didn’t:
> 
> app_len_test.hs:9:11:
>     Could not deduce (Foldable t0) arising from a use of ‘sum’
>     from the context (Applicative f, Foldable f)
>       bound by the type signature for
>                  app_len :: (Applicative f, Foldable f) => f a -> Int
>       at app_len_test.hs:8:12-52
>     The type variable ‘t0’ is ambiguous
>     Note: there are several potential instances:
>       instance Foldable ((,) a) -- Defined in ‘Data.Foldable’
>       instance GHC.Arr.Ix i => Foldable (GHC.Arr.Array i)
>         -- Defined in ‘Data.Foldable’
>       instance Foldable (Const m) -- Defined in ‘Data.Foldable’
>       ...plus four others
>     In the expression: sum
>     In the expression: sum $ pure 1
>     In an equation for ‘app_len’: app_len = sum $ pure 1
> 
> app_len_test.hs:9:17:
>     Could not deduce (Applicative t0) arising from a use of ‘pure’
>     from the context (Applicative f, Foldable f)
>       bound by the type signature for
>                  app_len :: (Applicative f, Foldable f) => f a -> Int
>       at app_len_test.hs:8:12-52
>     The type variable ‘t0’ is ambiguous
>     Note: there are several potential instances:
>       instance Data.Monoid.Monoid a => Applicative ((,) a)
>         -- Defined in ‘Control.Applicative’
>       instance Applicative ((->) a) -- Defined in ‘Control.Applicative’
>       instance Control.Arrow.Arrow a =>
>                Applicative (Control.Arrow.ArrowMonad a)
>         -- Defined in ‘Control.Applicative’
>       ...plus 14 others
>     In the second argument of ‘($)’, namely ‘pure 1’
>     In the expression: sum $ pure 1
>     In an equation for ‘app_len’: app_len = sum $ pure 1
> 
> Can anyone help me understand what I’m missing?
> 
> Thanks, and have a great weekend,
> -db
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4291 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151030/3ae6219b/attachment.bin>


More information about the Haskell-Cafe mailing list