문제

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