1
0
Fork 0
pr3-ws202627/haskell/05-geometry/geometry.hs

33 lines
1.1 KiB
Haskell

data Shape a = Circle {radius :: a} | Rectangle {height :: a, width :: a} deriving (Show)
area :: (Floating a) => Shape a -> a
area (Rectangle h w) = h * w
area (Circle r) = r ^ 2 * pi
circumference :: (Floating a) => Shape a -> a
circumference (Circle r) = 2 * pi * r
circumference (Rectangle h w) = 2 * h + 2 * w
scale :: (Floating a) => a -> Shape a -> Shape a
scale f (Circle r) = Circle (r * f)
scale f s = (scaleWidth f . scaleHeight f) s
scaleWidth :: (Num a) => a -> Shape a -> Shape a
scaleWidth f s = s {width = width s * f}
scaleHeight :: (Num a) => a -> Shape a -> Shape a
scaleHeight f s = s {height = height s * f}
instance (Show a) => Show (Shape a) where
show (Circle r) = "Circle with radius " ++ show r
show (Rectangle h w) = "Rectangel with height " ++ show h ++ " and with " ++ show w
class Meassurable s where
area2 :: (Floating a) => s a -> a
instance Meassurable Shape where
area2 (Rectangle h w) = h * w
area2 (Circle r) = r ^ 2 * pi
messurementSummary :: (Meassurable a, show a) => a -> String
messurementSummary m = "The area of " ++ "is " area2 m