質問

I have a very simple data and I want to use Show Typeclass to give it a nice output.

data Fruit = Apple | Orange Int 

instance Show Fruit
         where
            show Apple    = "Hey Apple"
            show Orange a = "X number of Orange"

This gives the following error but I don't know how to fix it:

Equations for `show' have different numbers of arguments
役に立ちましたか?

解決

You just forgot some parens :)

instance Show Fruit where
  show Apple      = "Hey Apple"
  show (Orange a) = show a ++ " number of Orange"

Patterns like Orange a need parens to disambiguate from something like Apple a where we really have two arguments.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top