Haskell"show适用于太多类型的参数"

mat*_*tio 4 haskell

我试图在haskell中复制UNIX程序wc.为了使这更容易,我创建了一个类型:

data WCResult = WCResult {
                      wordCount :: Int,
                      fileName  :: String
                     } --deriving (Show)

instance Show (WCResult x y) where
    show (WCResult x y) = show x ++ " " ++ y
Run Code Online (Sandbox Code Playgroud)

当我尝试运行这个程序时,我得到了

wc.hs:9:15:
`WCResult' is applied to too many type arguments
In the instance declaration for `Show (WCResult x y)'
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么?

Jos*_*Lee 14

该类型WCResult不带任何参数 - 您将类型构造函数与数据构造函数混淆,后者确实接受了参数:

instance Show WCResult where
    show (WCResult x y) = show x ++ " " ++ y
Run Code Online (Sandbox Code Playgroud)