Why does the 2-tuple Functor instance only apply the function to the second element?

Peter Hall picture Peter Hall · Nov 18, 2012 · Viewed 8.3k times · Source
import Control.Applicative

main = print $ fmap (*2) (1,2)

produces (1,4). I would expect it it to produce (2,4) but instead the function is applied only to the second element of the tuple.

Update I've basically figured this out almost straight away. I'll post my own answer in a minute..

Answer

Landei picture Landei · Nov 18, 2012

Let me answer this with a question: Which output do you expect for:

main = print $ fmap (*2) ("funny",2)

You can have something as you want (using data Pair a = Pair a a or so), but as (,) may have different types in their first and second argument, you are out of luck.