new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
33
Task/Classes/Haskell/classes-1.hs
Normal file
33
Task/Classes/Haskell/classes-1.hs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
class Shape a where
|
||||
perimeter :: a -> Double
|
||||
area :: a -> Double
|
||||
{- A type class Shape. Types belonging to Shape must support two
|
||||
methods, perimeter and area. -}
|
||||
|
||||
data Rectangle = Rectangle Double Double
|
||||
{- A new type with a single constructor. In the case of data types
|
||||
which have only one constructor, we conventionally give the
|
||||
constructor the same name as the type, though this isn't mandatory. -}
|
||||
|
||||
data Circle = Circle Double
|
||||
|
||||
instance Shape Rectangle where
|
||||
perimeter (Rectangle width height) = 2 * width + 2 * height
|
||||
area (Rectangle width height) = width * height
|
||||
{- We made Rectangle an instance of the Shape class by
|
||||
implementing perimeter, area :: Rectangle -> Int. -}
|
||||
|
||||
instance Shape Circle where
|
||||
perimeter (Circle radius) = 2 * pi * radius
|
||||
area (Circle radius) = pi * radius^2
|
||||
|
||||
apRatio :: Shape a => a -> Double
|
||||
{- A simple polymorphic function. -}
|
||||
apRatio shape = area shape / perimeter shape
|
||||
|
||||
main = do
|
||||
print $ apRatio $ Circle 5
|
||||
print $ apRatio $ Rectangle 5 5
|
||||
{- The correct version of apRatio (and hence the correct
|
||||
implementations of perimeter and area) is chosen based on the type
|
||||
of the argument. -}
|
||||
Loading…
Add table
Add a link
Reference in a new issue