new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
23
Task/Classes/Haskell/classes-2.hs
Normal file
23
Task/Classes/Haskell/classes-2.hs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
data Shape = Rectangle Double Double | Circle Double
|
||||
{- This Shape is a type rather than a type class. Rectangle and
|
||||
Circle are its constructors. -}
|
||||
|
||||
perimeter :: Shape -> Double
|
||||
{- An ordinary function, not a method. -}
|
||||
perimeter (Rectangle width height) = 2 * width + 2 * height
|
||||
perimeter (Circle radius) = 2 * pi * radius
|
||||
|
||||
area :: Shape -> Double
|
||||
area (Rectangle width height) = width * height
|
||||
area (Circle radius) = pi * radius^2
|
||||
|
||||
apRatio :: Shape -> Double
|
||||
{- Technically, this version of apRatio is monomorphic. -}
|
||||
apRatio shape = area shape / perimeter shape
|
||||
|
||||
main = do
|
||||
print $ apRatio $ Circle 5
|
||||
print $ apRatio $ Rectangle 5 5
|
||||
{- The value returned by apRatio is determined by the return values
|
||||
of area and perimeter, which just happen to be defined differently
|
||||
for Rectangles and Circles. -}
|
||||
Loading…
Add table
Add a link
Reference in a new issue