Haskell Flashcards
What function has this type signature?
(b -> b -> c) -> (a -> b) -> a -> a -> c
on
found in Data.Function
How can you use the on
function from Data.Function
?
((+) `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
What is a more optimal version of nub
and where is it found?
nubOrd
in Data.Containers.ListUtils
What functions are available in Data.Set
?
empty :: Set a
- Returns empty set of any typesingleton :: a -> Set a
- Returns a set containing a singular valuefromList :: Ord a => [a] -> Set a
- Creates a list from a listinsert :: Ord a => a -> Set a -> Set a
- Inserts a new value in the setmember :: Ord a => a -> Set a -> Bool
- Checks if a value is in a set
What functions are available in Data.Map
?
(!) :: 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 keyempty :: Map k a
- Returns empty map of any typesingleton :: k -> a -> Map k a
- Returns a map containing a single key and valueinsert :: Ord k => k -> a -> Map k a -> Map k a
- Return a new map with a new key and value inserted
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
foldr
and foldl
What function has this type signature?
(a -> b) -> [a] -> [b]
map
and fmap
What function has this type signature?
Functor t => (a -> b) -> t a -> t b
fmap
and <$>
How is Functor
and .
linked?
.
is equivalent to fmap
in the Functor (x ->)
definition
What are the fmap
laws?
fmap id = id
fmap f . fmap g = fmap (f . g)
What function has this type signature?
Applicative t => (a -> b -> c) -> t a -> t b -> t c
liftA2
What is the applicative version of fmap
liftA
from Control.Applicative
What function has this type signature?
Applicative t => a -> t a
pure
What function has this type signature?
(a -> b) -> a -> b
$
What is the liftA
definition using liftA2
?
liftA f mx = liftA2 ($) (pure f) mx
What function has this type signature?
Applicative t => t (a -> b) -> t a -> t b
<*>
What is the definition of <*>
using liftA2
?
liftA2 ($)
What is the liftA
definition using <*>
?
liftA f mx = pure f <*> mx
What is the liftA2
definition using <*>
?
liftA2 f mx my = f <$> mx <*> my
What are the two ways that a Monad
can be implemented?
join
and >>=
What function has this type signature?
Monad m => m (m a) -> m a
join
What is join
equivalent to in Monad []
?
concat
What function has this type signature?
Monad m => m a -> (a -> m b) -> m b
>>=
What is the >>=
definition using join
?
mx >>= f = join (fmap f mx)
What is the join
definition using >>=
?
join mmx = mmx >>= id
What is the liftM
definition?
liftM f mx = mx >>= (\x -> pure (f x))
What function has this type signature?
Monad m => (a -> b) -> m a -> m b
liftM
What function has this type signature?
Monad m => (a -> b -> c) -> m a -> m b -> m c
liftM2
What is the liftM2
definition?
liftM2 f mx my = mx >>= (\x -> fmap (f x) my)