Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top