[Haskell-cafe] Is it possible to represent such polymorphism?

Yves Parès limestrael at gmail.com
Sun Oct 2 15:16:00 CEST 2011


2-tuple and 3-tuple *are not the same type*.
So to do this you must use typeclasses.
Plus you have to deal with the type parameters

class To3Tuple a where
   expand :: a -> (Int, Int, Int)

instance To3Tuple (Int, Int, Int) where
   expand = id

instance To3Tuple (Int, Int) where
   expand (x,y) = (x,y,1)


Here I had to force my tuples to be tuples of integers.
It's more complicated if you want polymorphism.


2011/10/2 Du Xi <sdiyazg at sjtu.edu.cn>

> --I tried to write such polymorphic function:
>
> expand (x,y,z) = (x,y,z)
> expand (x,y) = (x,y,1)
>
> --And it didn't compile. Then I added a type signature:
>
> expand::a->b
> expand (x,y,z) = (x,y,z)
> expand (x,y) = (x,y,1)
>
> --It still didn't compile. I think the reason is that the following is
> disallowed:
>
> f::a->b
> f x = x
>
> --Is it possible to get around this and write the "expand" function? Of
> course, x and y may be of different types
>
>
>
> ______________________________**_________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/**mailman/listinfo/haskell-cafe<http://www.haskell.org/mailman/listinfo/haskell-cafe>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111002/5699b3eb/attachment.htm>


More information about the Haskell-Cafe mailing list