Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 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. -}
|
||||
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. -}
|
||||
21
Task/Classes/Haskell/classes-3.hs
Normal file
21
Task/Classes/Haskell/classes-3.hs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class Example (x) # 'x' is a field in class
|
||||
|
||||
# method definition
|
||||
method double ()
|
||||
return 2 * x
|
||||
end
|
||||
|
||||
# 'initially' block is called on instance construction
|
||||
initially (x)
|
||||
if /x # if x is null (not given), then set field to 0
|
||||
then self.x := 0
|
||||
else self.x := x
|
||||
end
|
||||
|
||||
procedure main ()
|
||||
x1 := Example () # new instance with default value of x
|
||||
x2 := Example (2) # new instance with given value of x
|
||||
write (x1.x)
|
||||
write (x2.x)
|
||||
write (x2.double ()) # call a method
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue