haskell notes Flashcards

1
Q

Functor laws

A

– preserve identity law
fmap id = id

– preserve function composition
fmap (g . h) = fmap g . fmap h

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

Applicative laws

A

– preserves identity function
pure id ^*> x = x

– preserves function application
pure (g x) = pure g ^*> pure x

– order of evaluation does not matter
x ^> pure y = pure (\g -> g y) ^> x

– is associative
x ^> (y ^> z) = (pure (.) ^> x ^> y) ^*> z

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

Monad laws

A

– bind returned value a function = apply function to value
return x&raquo_space;= f = f x

– bind return to a monadic value is the monadic value
mx&raquo_space;= return = mx

– binding is associative
(mx&raquo_space;= f)&raquo_space;= g = mx&raquo_space;= (\x -> (f x&raquo_space;= g))

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

Functor Class 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
5
Q

list Functor instance

A

instance Functor [] where
– fmap :: (a -> b) -> [a] -> [b]
fmap = map

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

Maybe Functor instance

A
instance Functor Maybe where
    -- fmap :: (a -> b) -> Maybe a -> Maybe b
    fmap _ Nothing = Nothing
    fmap g (Just x) = Just (g x)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

IO Functor instance

A

instance Functor IO where
– fmap :: (a -> b) -> IO a -> IO b
fmap g mx = do
x

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

Applicative class definition

A

class Functor f => 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
9
Q

Maybe Applicative instance

A

instance Applicative Maybe where
– pure:: a -> Maybe a
pure = Just

– ():: Maybe (a -> b) -> Maybe a -> Maybe b
Nothing _ = Nothing
(Just g) mx = fmap g mx

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

list Applicative instance

A

instance Applicative [] where
– pure :: a -> [a]
pure a = [a]

-- ()::[a->b] -> [a] -> [b]
gs  xs = [g x | g
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

IO Applicative instance

A

instance Applicative IO where
– pure:: a -> IO a
pure = return

-- () :: IO (a -> b) -> IO a -> IO b
mg  mx = do
        g
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Monad class definition

A
class Applicative m => Monad m where
    return :: a -> m a
    (>>=) :: m a -> (a -m b) -> m b
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Maybe Monad instance

A

instance Monad Maybe where
– (»=):: Maybe a -> (a -> Maybe b) -> Maybe b
Nothing&raquo_space;= _ = Nothing
(Just x)&raquo_space; f = f x

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

list Monad instance

A

instance Monad [] where
– (»=) :: [a] -> (a -> [b]) -> [b]
xs&raquo_space;= f = [y | x

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

Monoid class definition

A

class Monoid a where

mempty: :a
mappend: : a -> a-> a

mconcat:: [a] -> a
mconcat = foldr mappend mempty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Monoid laws

A

– identity 1
mempty mappend x = x

– identity 2
x mappend mempty = x

–associativity
x mappend (y mappend z) = (x mappend y) `mappend z

17
Q

list Monoid instance

A

instance Monoid [a] where
– mempty :: [a]
mempty = []

-- mappend:: a -> a-> a
mappend = (++)
18
Q

Maybe Monoid instance

A

instance Monoid (Maybe a) where
–mempty :: Maybe a
Nothing

-- mappend :: a -> a -> a
Nothing `mappend` my = my
mx `mappend` Nothing = mx
Just x `mappend` Just y = Just (x `mappend` y)
19
Q

Foldable class definition

A

class Foldable t where
fold::Monoid a => t a -> a
foldMap:: Monoid b => (a -> b) -> t a -> t b
foldr:: (a -> b -> b) -> b -> t a -> b
foldl:: (a -> b -> a) -> a -> t b -> a

20
Q

list Foldable instance

A

instance Foldable [] where
– fold::Monoid a => t a -> a
fold [] = mempty
fold (x:xs) = x mappend fold xs

-- foldMap:: Monoid b => (a -> b)  -> t a -> t b
foldMap _ [] = mempty
foldMap f (x:xs) = f x `mappend` foldMap f xs

-- foldr:: (a -> b -> b) -> b -> t a -> b
foldr _ v [] = v
foldr f v (x:xs) = f x (foldr f v xs)

-- foldl:: (a -> b -> a) -> a -> t b -> a
foldl _ v [] = v
foldl f v (x:xs) = foldl f (f v x) xs
21
Q

other Foldable primitive functions

A
null            :: t a -> Bool
length       :: t a -> Int
elem         :: Eq a => a -> t a -> Bool
maximum :: Ord a => t a -> a
minimum  :: Ord a => t a -> a
sum          :: Num a => t a -> a
product    :: Num a => t a -> a
22
Q

class definition Traversable

A

class (Functor t, Foldable t) => Traversable t where

traverse:: Applicative f => (a -> f b) -> t a -> f (t b)