Haskell: how to map a tuple?

quant_dev picture quant_dev · Mar 15, 2012 · Viewed 39.5k times · Source

In Haskell, I can easily map a list:

map (\x -> 2*x) [1,2]

gives me [2,4]. Is there any "mapTuple" function which would work like that?

mapTuple (\x -> 2*x) (1,2)

with the result being (2,4).

Answer

Rotsor picture Rotsor · Mar 15, 2012

Here's a rather short point-free solution:

import Control.Monad (join)
import Control.Arrow ((***))

mapTuple = join (***)