Reputation: 19
I've got this problem for college where I've got this:
data Expression a = Constant a
| Simetric (Expression a)
| Addition (Expression a) (Expression a)
| Subtraction (Expression a) (Expression a)
| Multiplication (Expression a) (Expression a)
and I've to write Expression a as an instance of Show.
Here's my code:
instance Show Expression where
show (Constant x) = (show x)
show (Simetric x) = "-" ++ (show x)
show (Addition x y) = (show x) ++ " + " ++ (show y)
show (Subtraction x y) = (show x) ++ " - " ++ (show y)
show (Multiplication x y) = (show x) ++ " * " ++ (show y)
And I get this error message on ghci when I try to compile:
Expecting one more argument to ‘Expression’
The first argument of ‘Show’ should have kind ‘*’,
but ‘Expression’ has kind ‘* -> *’
In the instance declaration for ‘Show Expression’
Failed, modules loaded: none.
Can someone explain me what's wrong in my code? I've got a final exam on Monday. Thanks for the help!
Upvotes: 1
Views: 56
Reputation: 12898
You should read compiler's messages, as it tries very hard to be helpful.
Expecting one more argument to ‘Expression’
Let's add some argument.
instance Show (Expression a) where
GHC complains again:
No instance for (Show a) arising from a use of ‘show’
Possible fix:
add (Show a) to the context of the instance declaration
Ok, let's add Show a
to the context:
instance Show a => Show (Expression a) where
Now it compiles fine. And by the way, you don't need braces around show
:
instance Show a => Show (Expression a) where
show (Constant x) = show x
show (Simetric x) = "-" ++ show x
show (Addition x y) = show x ++ " + " ++ show y
show (Subtraction x y) = show x ++ " - " ++ show y
show (Multiplication x y) = show x ++ " * " ++ show y
Upvotes: 3