-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeParameters.hs
47 lines (35 loc) · 1.47 KB
/
typeParameters.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
38
39
40
41
42
43
44
45
46
47
-- 03.06.2018
-- Type Parameters
-- Like value constructors can take some parameters and produce
-- a new value, type constructors can take types as parameters
-- to produce new types
data Maybe a = Nothing | Just a
-- the `a` here is the type parameter
-- because there is a type parameter involved, we call `Maybe`
-- a type constructor
-- if we want to explicitly pass a type as a type parameter, we
-- must do it in the type part of Haskell, which is usually
-- after the :: symbol
-- this can come in handy if we want a value of `Just 3` to have
-- the type `Maybe Int`
-- by default, Haskell will infer `(Num a) => Maybe a` for that
-- value
-- so we can use an explicit type annotation to restrict the type
-- Just 3 :: Maybe Int
-- type parameters are useful because they allow us to make data
-- types that can hold different things
-- for instance,
-- we could make a separate `Maybe` like data type for every type
-- that it could contain:
data IntMaybe = INothing | IJust Int
data StringMaybe = SNothing | SJust String
data ShapeMaybe = ShNothing | ShJust Shape
-- Notice that the type of Nothing is `Maybe a`
-- its type is polymorphic, which means that it features type
-- variables, namely the `a`
-- if some function requires a `Maybe Int` as a parameter, we can
-- give it a `Nothing`, because `Nothing` doesn't contain a value
-- anyway
-- `Maybe a` can act like a `Maybe Int`
-- similarly, the type of the empty list is [a]
-- an empty list can act like a list of anything