1 | initial version |
An Applicative List in Haskell is a list that supports the Applicative Functor type class. Here's a way to create an Applicative List in Haskell:
instance Applicative [] where
pure x = [x]
fs <*> xs = [f x | f <- fs, x <- xs]
In this code, we define an instance of the Applicative type class for the List data type ([]). The pure function takes a value x and returns a list with that single value. The <*> operator takes a list of functions (fs) and a list of values (xs) and creates a new list by applying each function to each value in turn.
Here's an example of how to use this Applicative List:
ghci> [(+1), (*2)] <*> [1, 2, 3]
[2, 3, 4, 2, 4, 6]
The result is a list of all possible combinations of applying the functions to the values. In this case, we get [2, 3, 4, 2, 4, 6], which is equivalent to [(1+1), (2+1), (3+1), (12), (22), (3*2)].