Applicative functors
Just like the notion of Functor
can be understood as a generalization of the map
function for lists to other type constructors, the notion of Applicative
functors is a generalization of the zip
/zipWith
functions for lists. However, for the sake of generality, it requires a variant of the zip
and zipWith
functions that we are already familiar with. Hence, we will explain that setup first before we dive into the general notion of Applicative
functors.
A family of zip functions
The standard library provides several zip
-like functions to merge lists. The basic one is defined as follows:
Prelude
zip :: [a] -> [b] -> [(a,b)]
zip [] ys = []
zip xs [] = []
zip (x:xs) (y:ys) = (x,y) : zip xs ys
This merges the two given lists into a new list by combining the elements at the corresponding positions into a tuple. The new list comes to an end...