blob: ae5a0c5560c9e3ae287eb2335736c7427bcdbb80 (
plain) (
blame)
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
30
31
32
33
34
35
|
{-# LANGUAGE Haskell2010 #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
module Records where
data Point = Point
{ x :: !Int
, y :: !Int
}
point :: Int -> Int -> Point
point x y = Point { x = x, y = y }
lengthSqr :: Point -> Int
lengthSqr (Point { x = x, y = y }) = x * x + y * y
lengthSqr' :: Point -> Int
lengthSqr' (Point { x, y }) = y * y + x * x
translateX, translateY :: Point -> Int -> Point
translateX p d = p { x = x p + d }
translateY p d = p { y = y p + d }
translate :: Int -> Int -> Point -> Point
translate x y p =
aux p
where
(dx, dy) = (x, y)
aux Point{..} = p { x = x + dx, y = y + dy }
|