-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquaternion.hs
More file actions
72 lines (43 loc) · 1.2 KB
/
Copy pathquaternion.hs
File metadata and controls
72 lines (43 loc) · 1.2 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
data Complex a
= !a :+ !a -- ^ forms a complex number from its real and imaginary
-- rectangular components.
deriving (Eq, Show, Read, Data, Generic, Generic1
, Functor, Foldable, Traversable)
data Complex a = !a :+ !a deriving (Eq,Read,Show)
let a = Complex 3 4
let a = 3 :+ 4
let b = 0.3 :+ 4
let c = (:+) 3 4
let d = [a,b,c]
addC a b
foldr addC d
let functionOfList f [x,y] = f x y
let f x y = x + y
functionOfList f [3,4]
functionOfList (:+) [1,2]
:t a
:t b
realPart a
realPart b
:{
let realZ :: (RealFloat a) => Complex a -> a
realZ (x :+ _) = x
:}
:{
let imZ :: (RealFloat a) => Complex a -> a
imZ (_ :+ y) = y
:}
let addC z1 z2 = (realZ z1 + realZ z2) :+ (imZ z1 + imZ z2)
addC a b
-- alternatively
:{
let addComplex :: (RealFloat a) => Complex a -> Complex a -> Complex a
addComplex (x :+ y) (x1 :+ y1) = (x + x1) :+ (y + y1)
:}
addComplex a b
let multz1z2 z1 z2 = (realZ z1 * realZ z2 - imZ z1 * imZ z2) :+ (realZ z1 * imZ z2 + realZ z2 * imZ z1)
multz1z2 a b
multz1z2 a c
data Complex a = !a :+ !a deriving (Eq, Show, Read, Functor, Foldable, Traversable)
:t functor
data Genre = Nonfiction | Novel | Biography deriving (Eq, Show)