[Haskell-beginners] Return a Foldable instance

Brandon S Allbery KF8NH allbery.b at gmail.com
Wed May 4 04:43:21 CEST 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 5/3/11 21:31 , Federico Mastellone wrote:
> I want to make a function that returns a Foldable instance, briefly,
> something like this:
> 
> import Data.Foldable
> 
> test :: Foldable f => f Int
> test = [1,2,3,4]
> 
> But I get this error:
> 
>     Couldn't match expected type `f' against inferred type `[]'
>         `f' is a rigid type variable bound by
>             the type signature for `test' at Test.hs:3:17
>     In the expression: [1, 2, 3, 4]
>     In the definition of `test': test = [1, 2, 3, ....]

Right; you're not returning a call-point-specified Foldable, you're
returning a list always.  I suspect you really want Traversable, not
Foldable, so you can map elements from list to arbitrary Traversable; the
reverse is then Data.Foldable.toList.  (All Traversables are Foldables.)

http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.3.1.0/Data-Traversable.html

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]    allbery.b at gmail.com
system administrator  [openafs,heimdal,too many hats]                kf8nh
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3AvUkACgkQIn7hlCsL25UVCACcCVHdZL8u2JwD0WZ0P4V6+u3+
XcQAoLI/mqsekYCA2SmlyAfMNpn5Kjem
=I0YQ
-----END PGP SIGNATURE-----



More information about the Beginners mailing list