-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.icl
More file actions
51 lines (40 loc) · 1.59 KB
/
testing.icl
File metadata and controls
51 lines (40 loc) · 1.59 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
module testing
import StdEnv
/* 1.
Create an instances +, -, <,<>, == for RGBColor
+ should add respective parameters
- should subtract respective parameters
== is true if all three parameters are equal
<> is false if all three parameters are equal, true otherwise.
< Compare them lexicographically (if reds are equal compare greens and so on)
*/
:: RGBColor = {r :: Int, g :: Int, b :: Int}
instance + RGBColor
where
(+) :: !RGBColor !RGBColor -> RGBColor
(+) x y = {r = x.r + y.r, g = x.g + y.g, b = x.b + y.b}
instance - RGBColor
where
(-) :: !RGBColor !RGBColor -> RGBColor
(-) x y = {r = x.r - y.r, g = x.g - y.g, b = x.b - y.b}
instance < RGBColor
where
(<) :: !RGBColor !RGBColor -> Bool
(<) x y = (x.r + x.g + x.b) < (y.r + y.g + y.b)
instance == RGBColor
where
(==) :: !RGBColor !RGBColor -> Bool
(==) x y = (x.r == y.r) && (x.g == y.g) && (x.b == y.b)
class Diff a | == a
where
(<>) infix 4 :: a a -> Bool | Diff a
(<>) x y = not (x == y)
toString
//Start = {r = 0, g = 0, b = 0} == {r = 0, g = 0, b = 0} // True
//Start = {r = 0, g = 0, b = 0} <> {r = 0, g = 0, b = 0} // False
//Start = {r = 30, g = 150, b = 231} == {r = 10, g = 30, b = 231} // False
//Start = {r = 30, g = 150, b = 231} - {r = 1, g = 1, b = 1} // {r = 29, g = 149, b = 230}
//Start = {r = 30, g = 150, b = 231} + {r = 1, g = 1, b = 1} // {r = 31, g = 152, b = 232}
//Start = {r = 30, g = 150, b = 231} < {r = 10, g = 30, b = 231} // False
//Start = {r = 30, g = 150, b = 231} < {r = 30, g = 150, b = 231} // False
//Start = {r = 30, g = 150, b = 231} < {r = 30, g = 151, b = 231} // True