diff --git a/part2.html b/part2.html index a0c222f..48e8dfa 100644 --- a/part2.html +++ b/part2.html @@ -166,7 +166,7 @@ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warni
  • 15.5 Validating Lists: traverse
  • 15.6 Sidenote: Traversable
  • 15.7 Dealing with Failure: Alternative
  • -
  • 15.8 Sidenote: Monads and Applicatives
  • +
  • 15.8 Sidenote: Applicatives in Context
  • 15.9 Quiz
  • 15.10 Exercises
  • @@ -3875,7 +3875,6 @@ liftA2 sumMoney (parseMoney "123" "e") (parseMoney "xxx liftA2 f (Ok x) (Errors e2) = Errors e2 liftA2 f (Errors e1) (Errors e2) = Errors (e1++e2)

    The definition of liftA2 for Validation shows that the errors are collected together left-to-right. This can be seen in the example above where the expression liftA2 congratulate checkedName checkedAge outputs the error ("Name too long") from checkedName first, and the error ("Too old") from checkedAge last.

    -

    PS. The same sort of idea has been implemented in the validation and either libraries.

    15.5 Validating Lists: traverse

    So far we’ve dealt with fixed-size things and Applicatives: we’ve applied a function of two or three arguments to some things. What if we have an arbitrary amount of inputs? What if we need to validate a list?

    @@ -4065,7 +4064,17 @@ TODO another example TODO explain `some` and `many` --> -

    15.8 Sidenote: Monads and Applicatives

    +

    15.8 Sidenote: Applicatives in Context

    +

    15.8.1 Why Applicatives?

    +

    There are multiple reasons for learning Applicatives, even if they do not provide any additional power over Monads. First off, as discussed in Lecture 13, the GHC standard library nowadays makes Applicative instances for all Monads compulsory. Thus a working Haskell programmer is bound to see lots of Applicative instances.

    +

    Secondly, you’ll bump into Applicative operators a lot even in Monadic code. Expressions like f <$> x <*> y <*> z can be useful in many Monadic contexts. Also, since the Traversable type class is built in terms of Applicative, you’ll often use applicative operations with it.

    +

    Thirdly, Applicatives are an excellent exercise in understanding functional design patterns. They marry the Functor pattern with the Monoid pattern, and Alternative brings in yet another Monoid-like dimension. Being able to efficiently work with Applicatives will make working with further abstractions like monad transformers or lenses easier.

    +

    Lastly, there are a couple of types that are Applicatives but not Monads. Validation is one example, and a very practical one at that. Without understanding of Applicative, we’d have no way to recognize and generalize operations over types like this. Another such type is ZipList.

    +

    15.8.2 Applicatives in the Wild

    +

    Even though we only looked at some very simple and concrete Applicatives in this lecture, there are plenty of Haskell libraries that use Applicatives for big things. Here are some examples.

    +

    The same sort of idea as our Validation Applicative has been implemented in the validation and either libraries.

    +

    There are multiple parser libraries that use Applicatives. For example, regex-applicative, optparse-applicative, yamlparse-applicative, json-stream, and so on.

    +

    15.8.3 Monads and Applicatives

    So what’s the relationship between an Monad and an Applicative? If an Applicative is also a Monad, the following laws hold:

    pure             === return