-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataConstructors.hs
29 lines (18 loc) · 1.25 KB
/
DataConstructors.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
-- In a data declaration, a type constructor is the thing on the left hand side of the equals sign.
-- The data constructor(s) are the things on the right hand side of the equals sign.
-- You use type constructors where a type is expected, and you use data constructors where a value is expected.
data Colour = Red | Green | Blue
-- Here, we have three data constructors.
-- Colour is a type constructor, and Green is a data constructor that contains a value of type Colour.
-- Similarly, Red and Blue are both data constructors that construct values of type Colour.
-- We could imagine spicing it up though!
data Colour' = RGB Int Int Int
--We still have just the type Colour, but RGB is not a value - it's a function takinf three Ins and returning a value!
-- RGB has the type
RGB :: Int -> Int -> Int -> Colour
-- RGB is a data constructor that is a function taking some values as its arguments, and then uses those to construct a new value.
-- In this case, if we apply RGB to three values, we get a colour value!
-- Prelude> RGB 12 92 27
-- #0c5c1b
-- We have constructed a value of type Colour by applying the data constructor.
-- A data constructor either contains a value like a variable would, or takes other values as its argument and creates a new value.