【发布时间】:2012-10-06 11:45:40
【问题描述】:
我写了下面的快速排序实现:
import Data.List (partition)
quicksort [] = []
quicksort (x:xs) =
let (smaller, notSmaller) = partition (< x) xs
in quicksort smaller ++ x : quicksort notSmaller
然后我想通过将fmap 应用于列表对来缩短对quicksort 的两个递归调用:
quicksort (x:xs) =
let (smaller, notSmaller) = fmap quicksort $ partition (< x) xs
in smaller ++ x : notSmaller
但显然,(a, a) 不是函子。这是为什么?我试着提供一个:
instance Functor (a, a) where
fmap f (x, y) = (f x, f y)
但是 ghci 不喜欢我的尝试:
Kind mis-match
The first argument of `Functor' should have kind `* -> *',
but `(a, a)' has kind `*'
In the instance declaration for `Functor (a, a)'
谁能向我解释这个错误?我尝试了各种“修复”,但都没有奏效。
是否可以使(a, a) 成为Functor 的实例?还是类型系统不够表达?
【问题讨论】:
标签: haskell typeclass functor type-systems