[Haskell-cafe] mapTuple
Grady Lemoine
grady.lemoine at gmail.com
Fri Jan 12 20:19:06 EST 2007
Is there anything in particular you're trying to accomplish? It seems
like this is the type of thing you'd accomplish with typeclasses if
you had a less general problem than you've presented. For example,
> mapShowTuple :: (Show a, Show b) => (a, b) -> (String, String)
> mapShowTuple (x, y) = (show x, show y)
That said, it would be nice do be able to do something a little more
general, but still with a specific typeclass, like
> mapNumOpTuple :: (Num a, Num b) => (Num c => c -> c) -> (a, b) -> (a, b)
> mapNumOpTuple f (x, y) = (f x, f y)
(This unfortunately fails to typecheck; GHC chokes with "Illegal
polymorphic or qualified type". On the other hand, I'm still pretty
new to Haskell myself, so maybe someone else knows how to write this
correctly without doing complex type hackery.)
It would also be nice to be able to generalize over all typeclasses,
e.g. (pseudo-code here)
mapTypeclassOpTuple :: for all typeclasses C ((C a, C b) => (C c => c
-> c) -> (a, b) -> (a, b))
but I don't even know how that would fit into Haskell's syntax. I
suspect it's an idea that's been discussed, and I just don't know the
term for it.
Anyway, if you can make your problem more specific, it might be easier to solve.
--Grady
On 1/11/07, Marco Túlio Gontijo e Silva <malebria at riseup.net> wrote:
> Hello,
>
> is there a way to defined something as a map to use in tuples? I tried
> this:
>
> mapTuple f (a, b) = (f a, f b)
>
> But the type inferred to it is not as generic as I wanted:
>
> mapTuple :: (t -> t1) -> (t, t) -> (t1, t1)
>
> Then I tried a different, but not much, implementation:
>
> mapTuple' f g (a, b) = (f a, g b)
> mapTuple f = mapTuple' f f
>
> But the inferred type was the same.
>
> Is there a way to define a function in which I can be able to do
> something as this?
>
> mapTuple show ("string", True)
>
> --
> malebria
> Marco Túlio Gontijo e Silva
> Correio (MSN): malebria at riseup.net
> Jabber (GTalk): malebria at jabber.org
> Telefone: 33346720
> Celular: 98116720
> Endereço:
> Rua Paula Cândido, 257/201
> Gutierrez 30430-260
> Belo Horizonte/MG Brasil
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list