forked from noelmarkham/learn-you-a-haskell-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-types-and-typeclasses.hs
37 lines (33 loc) · 1.34 KB
/
3-types-and-typeclasses.hs
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
{-
- For this exercise, we are dealing with a type for colours of the rainbow
- The typeclass is defined here, and note its English spelling.
- For more information on how this is done, look ahead to:
- http://learnyouahaskell.com/making-our-own-types-and-typeclasses
-
- Have a play with the Colour in ghci, try the succ and pred functions and so on.
-}
data Colour = Red | Orange | Yellow | Green | Blue | Indigo | Violet
deriving (Eq, Ord, Show, Bounded, Enum)
{-
- Again, you should be able to write these functions in one line,
- using the information from the chapter http://learnyouahaskell.com/types-and-typeclasses
- and the chapter before
-}
{-
- The Colour typeclass is of type Ord
- What is the "first" (or least) colour
-}
firstColour :: Colour
firstColour = minBound :: Colour
-- List the colours in reverse order
reverseColourOrder :: [Colour]
reverseColourOrder = reverse [minBound :: Colour .. maxBound :: Colour]
{-
- Mix two colours together, to produce the average value of the two.
- Example: paintMix Orange Green = Yellow
- If necessary, favour the "higher" value when computing the average.
- For example: paintMix Green Violet = Indigo
- Hint: Integer division can be performed with the quot function: quot 7 2 = 3
-}
paintMix :: Colour -> Colour -> Colour
paintMix c1 c2 = [c1 .. c2] !! quot (length [c1 .. c2]) 2