8. Declaring types and classes Flashcards

1
Q

What is the keyword for declaring a type? Give a simple example

A

type
i.e. type Pos = (Int, Int)
(must be capital P)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can the type keyword be used for declaring recursive types?

A

No, use the ‘data’ keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What keyword can be used for declaring new types? Give an example

A

Data
i.e. data Bool = False | True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to declare a new type containing of a single argument? Give an example

A

newtype
i.e. newtype Nat = N Int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give an example of a recursive type

A

data Nat = Zero | Succ Nat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to declare a class? Give an example

A

class
i.e. class Eq a where
(==), (/=) :: a -> a -> Bool
x /= y = not (x == y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to define a type as an instance of a class?

A

instance Eq Bool where
False == False = True
True == True = True
_ == _ = False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to make a type an instance of a class?

A

deriving
data Bool = False | True
deriving (Eq, Ord, Show, Read)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly