patterns of computation Flashcards

1
Q

how would we define a tree type

A

data Tree a = Leaf a
| Node (Tree a) (Tree a)

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

what does functor actually do

A

allows you do use map with any data type not just lists

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

what does functor represent

A

a type that can be mapped over

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

what is the functor definition

A

class Functor f where
fmap :: (a -> b) -> f a -> f b

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

what does the applicative functor do

A

generalises functor to multiple arguments functions

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

in which case do we use the applicative functor

A

when we want the other arguments for a function to come dynamically from data (e.g. another list)

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

what is the (worse) alternative to using the applicative functor

A

zip the data lists together and pass to map as a tuple, then deconstruct the type inside of the function being passed

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

what is the definition for the applicative functor

A

class Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b

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

what is the role of ‘pure’ in an applicative functor

A

lifts the value into the context of the function

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

what is the role of <*> in an applicative functor

A

its an operator that applies a function inside a context to a value inside a context

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

what does monad do

A

generalises the flow of computation depending on the result of functions in that computation allowing each step to influence the next

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

what is the definition for monad

A

class Monad m where
(»=) :: m a -> (a -> m b) -> m b
(») :: m a -> m b -> m b
pure :: a -> m a

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

what does “(»=) :: m a -> (a -> m b) -> m b” do in monad

A

takes a value inside a monad and the function that creates a new nomadic value and applies it to transform the value

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

what does “(») :: m a -> m b -> m b” do in monad

A

sequences two monadic actions ignoring the result of the first

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

(»)

A

used for when the variant takes no input argument (usually for io)

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

(»=)

A

chainMaybe operator

16
Q

how are io and monad related

A

io extends the monad context as it gives it a way to chain functions together so data gets passed on to the next