Possible Duplicate:
Making (a, a) a Functor
I wrote the following implementation of quicksort:
import Data.List (partition)
quicksort [] = []
quicksort (x:xs) =
let (smaller, notSmaller) = partition (< x) xs
in quicksort smaller ++ x : quicksort notSmaller
Then I wanted to abbreviate the two recursive calls to quicksort
by applying fmap
to the list pair:
quicksort (x:xs) =
let (smaller, notSmaller) = fmap quicksort $ partition (< x) xs
in smaller ++ x : notSmaller
But apparently, (a, a)
is not a functor. Why is that? I tried to provide one:
instance Functor (a, a) where
fmap f (x, y) = (f x, f y)
But ghci did not like my attempt:
Kind mis-match
The first argument of `Functor' should have kind `* -> *',
but `(a, a)' has kind `*'
In the instance declaration for `Functor (a, a)'
Could anyone explain that error to me? I tried various "fixes", but none of them worked.
Is it possible to make (a, a)
an instance of Functor
? Or is the type system not expressive enough?