The Haskell code below works fine.
data Point = Point Float Float deriving (Show)
data Shape = Circle Point Float
surface :: Shape -> Float
surface (Circle _ r) = pi * r ^ 2
Result:
*Main> surface $ Circle (Point 0 0) 10
314.15927
The Haskell code below does not work. Why? How to write surface function for Shape – Circle correctly?
data Point = Point Float Float deriving (Show)
data Radius = Radius Float deriving (Show)
data Shape = Circle Point Radius
surface :: Shape -> Float
surface (Circle _ (Radius r)) = pi * (Radius r) ^ 2
Your last line is constructing a
Radiusobject and raising that to a power. Since you haven’t defined the power operator forRadius, that can’t work. Remove the constructor call: