Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE GADTs #-}
- class Shape s where
- area :: s -> Double
- perimeter :: s -> Double
- data Rectangle = Rectangle {
- width :: Double,
- height :: Double
- } deriving Show
- instance Shape Rectangle where
- area rectangle = (width rectangle) * (height rectangle)
- perimeter rectangle = 2 * ((width rectangle) + (height rectangle))
- data Circle = Circle {
- radius :: Double
- } deriving Show
- instance Shape Circle where
- area circle = pi * (radius circle) * (radius circle)
- perimeter circle = 2.0 * pi * (radius circle)
- r=Rectangle 10.0 3.0
- c=Circle 10.0
- list=[WrapShape r,WrapShape c]
- data ShapeWrapper where
- WrapShape :: Shape s => s -> ShapeWrapper
- getArea :: ShapeWrapper -> Double
- getArea (WrapShape s) = area s
- getPerimeter :: ShapeWrapper -> Double
- getPerimeter (WrapShape s) = perimeter s
- areas = map getArea list
- perimeters = map getPerimeter list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement