如何为数据编写"显示"类型类

Mik*_* S. 2 haskell functional-programming typeclass

我有一个非常简单的data,我想使用ShowTypeclass给它一个很好的输出.

data Fruit = Apple | Orange Int 

instance Show Fruit
         where
            show Apple    = "Hey Apple"
            show Orange a = "X number of Orange"
Run Code Online (Sandbox Code Playgroud)

这给出了以下错误,但我不知道如何解决它:

Equations for `show' have different numbers of arguments
Run Code Online (Sandbox Code Playgroud)

Dan*_*zer 8

你刚忘了一些parens :)

instance Show Fruit where
  show Apple      = "Hey Apple"
  show (Orange a) = show a ++ " number of Orange"
Run Code Online (Sandbox Code Playgroud)

Orange a需要parens的模式可以消除歧义,例如Apple a我们真正有两个参数的地方.