Get Programming With Haskell

Unit3 - Programming in types

chapter 17 Semigroups and Monoids

1.Monoid requires me to specify an identity element.

Unit5 - Working with type in a context

  • Use Functor, when I have (a -> b) in context.
  • Use applicative, when I have (f a -> b) in context.
  • Use applicative, for Maybe (Int -> Double).
  • Use Monad, when (a -> f b) is in context.

chapter27 - The Functor typeclass

  • Transform the types of values inside a Maybe
  • Types of kind * -> * are parameterized types that take just one type parameter.
  • Member of Functor include List, Map, Maybe and IO.
  • [Int], Maybe Int, and IO Int can all use the same core functions.
  • Functor’s <$> provides a common interface to apply any function to a

value in a context.

  • fmap type signature

    fmap :: Functor f => (a -> b) -> f a -> f b
    

chapter28 - A peek at the applicative typeclass

  • applicative type signature. <*> operator (pro-

nounced app)

(<*>) :: Applicative f => f (a -> b) -> f a -> f b
  • limitation of Functor's fmap is that it only works on single-argument functions.

  • Using Functor's <$> operator for partial application in a context

    maybeInc = (+) <$> Just 1
    
  • <*> (app operator)

  • <$> and <*> to chain together any number of arguments.

chapter29 - List as context: A deeper look at the applicative typeclass

  • applicative typeclass

    class Functor f => Applicative f where
       (<*>) :: f (a -> b) -> f a -> f b
       pure :: a -> f a
    
  • The pure method has a way to take an ordinary type and put it in a context.

  • In nondeterministic computing, we are computing multiple possibilities all at once.

  • A list as a container is a sequence of values that can hold any type. Each item in

the list points to the next one or to the empty list.

  • A list as a context represents a set of possibilities. Think of a list as a context as

being a single variable that can contain many possible values.

chapter30 - Introducing the Monad typeclass

  • Functor allows me to change individual values in a context.

  • Applicative increases my power by enabling me to use partial application in a context.

  • monad typeclass

    class Applicative m => Monad (m :: * -> *) where
       (>>=) :: m a -> (a -> m b) -> m b
       (>>) :: m a -> m b -> m b
       return :: a -> m a
       fail :: String -> m a
    
  • The Monad type class predates the Applicative type class, so the return method exists for legacy reasons.

  • Unlike Maybe, we can’t trivially use pattern matching to access values inside the IO context.

chapter31 - Making Monads easier with do-notation

  • (>>) allows me to perform an IO action and chain it with another action, ignoring

its value.

  • (>>=) allows me to perform an IO action and then hand off the return value of that

function to another waiting for a value.

  • (
    \x
    -> return (func x)) allows me to take an ordinary function and have it work in

the context of IO.

  • Do-notation is syntactic sugar for using >>, >>=, and (
    \x
    -> return

(func x)).