Reputation: 26048
I have the following simple code:
data Shape = Circle Float Float Float | Rectangle Float Float Float Float deriving (Show)
surface :: Shape -> Float
surface (Circle _ _ r) = pi * r ^ 2
main = putStrLn $ surface $ Circle 10 20 30
It complains:
Couldn't match expected type `String' with actual type `Float'
In the second argument of `($)', namely `surface $ Circle 10 20 30'
How do I get rid of the error? I also would like to "add" show
method to Shape
and override it so that I can represent Shape
on the screen (printed) whatever I want.
Upvotes: 1
Views: 105
Reputation: 9946
You need to add show:
main = putStrLn $ show $ surface $ Circle 10 20 30
If you want your own Show method, don't derive Show:
data Shape = Circle Float Float Float
| Rectangle Float Float Float Float
instance Show Shape where
show (Circle _ _ r) = show r
show (Rectangle r _ _ _) = show r
main = putStrLn $ show $ Circle 10 20 30
Upvotes: 4