-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.hs
More file actions
29 lines (24 loc) · 857 Bytes
/
types.hs
File metadata and controls
29 lines (24 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Shapes
(
Point (..),
Shape (..),
area,
nudge,
baseCircle,
baseRect
) where
main = putStrLn "Hello World"
-- Point类型,表示平面中的点
data Point = Point Float Float deriving (Show)
-- Shape 是类型,它的值为Circle和Rectangle
data Shape = Circle Point Float | Rectangle Point Point deriving (Show)
area :: Shape -> Float
area (Circle _ r) = pi * r ^ 2
area (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs $ y2 - y1)
nudge :: Shape -> Float -> Float -> Shape
nudge (Circle (Point x y) r) a b = Circle (Point (x + a) (y + b)) r
nudge (Rectangle (Point x1 y1) (Point x2 y2)) a b = Rectangle (Point (x1 + a) (y1 + b)) (Point (x2 + a) (y2 + b))
baseCircle :: Float -> Shape
baseCircle = Circle (Point 0 0)
baseRect :: Float -> Float -> Shape
baseRect width height = Rectangle (Point 0 0) (Point width height)