Haskell: Functors, Applicatives and Monads

Relationship:


|Functors |
|__________________ |
|Applicatives | |
|_______________ | |
|Monnads | | |
|| | |
|
| |
|
________________|

Functors:

  • Definition:

    A class instance with fmap implemented.
    It should satisify the law:
    fmap id = id : there exits an identity function s.t. the function does not change anyhting about the container.
    The law make sure that fmap does not change the structure of the container and it implies fmap (g . h) = (fmap g) . (fmap h) (distrubitivity?)
    • Intuition:

      Container(type function) of (not concrete, i.e. the type should be a type function that accepts another type as parameter)types where fmap can be used (a function that a takes in a function f :: (a -> b) where a and b are non concrete types and output a function f' :: (functorInstance a -> functorInstance b). i.e. apply the function f to every element in the coontainer).
    • Syntax:

      1
      2
      3
      instance Functor $TYPE_FUNCTION where
      fmap :: (a->b) -> $TYPE_FUNCTION a ->$TYPE_FUNCTAION b
      $FMAP_DEFINITION_HERE
      • Usage:

        Error checking, e.g. a function that applies to Either e could take a possible failure and pass some useful information about the failure.
      • Notable Examples:

        • Tree
        • Map
        • Sequence
        • []
        • Maybe
        • Either

Author

ChiatzenW

Posted on

2022-06-21

Updated on

2022-06-21

Licensed under

Comments