haskell - Representing data as a string -
i have 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' actual type `float' in second argument of `($)', namely `surface $ circle 10 20 30' how rid of error? "add" show method shape , override can represent shape on screen (printed) whatever want.
you need add show:
main = putstrln $ show $ surface $ circle 10 20 30 if want own show method, don't derive show:
data shape = circle float float float | rectangle float float float float instance show shape show (circle _ _ r) = show r show (rectangle r _ _ _) = show r main = putstrln $ show $ circle 10 20 30
Comments
Post a Comment