2012-10-24 95 views
3

新手問題,爲什麼這在Haskell中不正確?哈斯克爾基礎類

class BasicEq a where 
    isEqual :: a -> a -> Bool 
    isNotEqual :: a -> a -> Bool 
    isNotEqual = not . isEqual 

回答

9

讓我們打開了GHC提示,並期待在類型的東西:

Prelude> :t not 
not :: Bool -> Bool 
Prelude> :t (not .) 
(not .) :: (a -> Bool) -> a -> Bool 

所以你可以看到(not .)需要a -> Bool,不是a -> a -> Bool。我們可以雙擊該功能成分得到一個工作版本:

Prelude> :t ((not .) .) 
((not .) .) :: (a -> a1 -> Bool) -> a -> a1 -> Bool 

所以,正確的定義是:

isNotEqual = (not .) . isEqual 

或等價,

isNotEqual x y = not $ isEqual x y 
isNotEqual = curry $ not . uncurry isEqual 

等等。

6

.的運營商期望兩個 「一元函數」( 「× - > Y」),但isEqual是 「二進制功能」( 「× - >ý - > Z」),因此它不會工作。你可能只是不使用免費的點對點形式:

isNotEqual x y = not $ isEqual x y