Haskell Flashcards

1
Q

What function has this type signature?

(b -> b -> c) -> (a -> b) -> a -> a -> c

A

on found in Data.Function

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

How can you use the on function from Data.Function?

A

((+) `on` f) x y = f x + f y
To sort by the second value in a list of pairs - sortBy (compare `on` snd)
This is also equivalent to sortOn snd

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

What is a more optimal version of nub and where is it found?

A

nubOrd in Data.Containers.ListUtils

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

What functions are available in Data.Set?

A

empty :: Set a - Returns empty set of any type
singleton :: a -> Set a - Returns a set containing a singular value
fromList :: Ord a => [a] -> Set a - Creates a list from a list
insert :: Ord a => a -> Set a -> Set a - Inserts a new value in the set
member :: Ord a => a -> Set a -> Bool - Checks if a value is in a set

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

What functions are available in Data.Map?

A

(!) :: Ord k => Map k a -> k -> a - Returns the value at a key (partial)
lookup :: Ord k => Map k a -> k -> Maybe a - Returns a maybe of the value at a key
empty :: Map k a - Returns empty map of any type
singleton :: k -> a -> Map k a - Returns a map containing a single key and value
insert :: Ord k => k -> a -> Map k a -> Map k a - Return a new map with a new key and value inserted

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

What function has this type signature?

Foldable t => (a -> b -> b) -> b -> t a -> b

Hint: A List is an example of a foldable structure

A

foldr and foldl

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

What function has this type signature?

(a -> b) -> [a] -> [b]

A

map and fmap

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

What function has this type signature?

Functor t => (a -> b) -> t a -> t b

A

fmap and <$>

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

How is Functor and . linked?

A

. is equivalent to fmap in the Functor (x ->) definition

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

What are the fmap laws?

A

fmap id = id
fmap f . fmap g = fmap (f . g)

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

What function has this type signature?

Applicative t => (a -> b -> c) -> t a -> t b -> t c

A

liftA2

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

What is the applicative version of fmap

A

liftA from Control.Applicative

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

What function has this type signature?

Applicative t => a -> t a

A

pure

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

What function has this type signature?

(a -> b) -> a -> b

A

$

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

What is the liftA definition using liftA2?

A

liftA f mx = liftA2 ($) (pure f) mx

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

What function has this type signature?

Applicative t => t (a -> b) -> t a -> t b

17
Q

What is the definition of <*> using liftA2?

A

liftA2 ($)

18
Q

What is the liftA definition using <*>?

A

liftA f mx = pure f <*> mx

19
Q

What is the liftA2 definition using <*>?

A

liftA2 f mx my = f <$> mx <*> my

20
Q

What are the two ways that a Monad can be implemented?

A

join and >>=

21
Q

What function has this type signature?

Monad m => m (m a) -> m a

22
Q

What is join equivalent to in Monad []?

23
Q

What function has this type signature?

Monad m => m a -> (a -> m b) -> m b

24
Q

What is the >>= definition using join?

A

mx >>= f = join (fmap f mx)

25
Q

What is the join definition using >>=?

A

join mmx = mmx >>= id

26
Q

What is the liftM definition?

A

liftM f mx = mx >>= (\x -> pure (f x))

27
Q

What function has this type signature?

Monad m => (a -> b) -> m a -> m b

28
Q

What function has this type signature?

Monad m => (a -> b -> c) -> m a -> m b -> m c

29
Q

What is the liftM2 definition?

A

liftM2 f mx my = mx >>= (\x -> fmap (f x) my)