下面的 Haskell 代码运行良好。
data Point = Point Float Float deriving (Show)
data Shape = Circle Point Float
surface :: Shape -> Float
surface (Circle _ r) = pi * r ^ 2
结果:
*Main> surface $ Circle (Point 0 0) 10
314.15927
下面的 Haskell 代码不起作用。为什么?如何正确编写表面函数Shape
?Circle
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