-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTypes.hs
More file actions
73 lines (54 loc) · 2.26 KB
/
Copy pathDataTypes.hs
File metadata and controls
73 lines (54 loc) · 2.26 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module DataTypes where
import Data.Function
-- | Data type describing the days of the week
data Day = Mon | Tue | Wed | Thurs | Fri | Sat | Sun
deriving (Eq, Enum, Ord, Read)
instance Show Day where
show Mon = "Monday"
show Tue = "Tuesday"
show Wed = "Wednesday"
show Thurs = "Thursday"
show Fri = "Friday"
show Sat = "Saturday"
show Sun = "Sunday"
main' = do
print Mon -- Prints "Monday"
print [Tue .. Sat] -- Prints [Tuesday,Wednesday,Thursday,Friday,Saturday]
print $ Mon == Tue -- Prints False
print $ Mon >= Tue -- Prints False
--------------------------------------------------------------------------------
data ComplexInt = ComplexInt { realI :: Int
, imagI :: Int
} deriving (Eq)
instance Show ComplexInt where
show c = show (realI c) ++ " + " ++ show (imagI c) ++ "i"
instance Num ComplexInt where
a + b = ComplexInt (realI a + realI b) (imagI a + imagI b)
(ComplexInt x y) * (ComplexInt u v) = ComplexInt (x * u - y * v) (x * v + y * u)
abs c = ComplexInt (abs $ realI c) (abs $ imagI c)
negate c = ComplexInt (negate $ realI c) (negate $ imagI c)
signum c = ComplexInt (signum $ realI c) (signum $ imagI c)
fromInteger n = ComplexInt (fromIntegral n) 0
cmapInt :: (Int -> Int) -> ComplexInt -> ComplexInt
cmapInt f c = ComplexInt (f $ realI c) (f $ imagI c)
main'' = print $ ComplexInt 1 2
--------------------------------------------------------------------------------
data Complex a = Complex { real :: a
, imag :: a
} deriving (Eq)
instance Show a => Show (Complex a) where
show c = show (real c) ++ " + " ++ show (imag c) ++ "i"
instance Num a => Num (Complex a) where
a + b = Complex (real a + real b) (imag a + imag b)
(Complex x y) * (Complex u v) = Complex (x * u - y * v) (x * v + y * u)
abs = fmap abs -- Identical to the Num Instance above but
negate = fmap negate -- uses the fmap function below to clean the code.
signum = fmap signum
fromInteger n = fromInteger n +: 0
instance Functor Complex where
fmap f c = Complex (f $ real c) (f $ imag c)
-- | Infix constructor operator
(+:) :: a -> a -> Complex a
infix 6 +:
r +: i = Complex r i
main = print $ (1 +: 2) + (2 +: 1)