Home > Enterprise >  Applying a function in a nested context to a value in a nested context in haskell
Applying a function in a nested context to a value in a nested context in haskell

Time:11-01

Consider the following Applicative context f. I have a list of functions wrapped around this context F = f [a -> a] and a list of values wrapped around the same context i.e. V = f [a]. Now I want to apply the functions in F to the values in V. How can this be achieved?

I have tried the following :

1. (F <*>) <*> V
2. (V <*>) (F <*>)

But for some reason the types won't match.

CodePudding user response:

The types won't match in 1 because both <*>s are at the same "layer", and they won't match in 2 because you're trying to call the values as functions. (There are other problems too, but that should be enough to see why they shouldn't work.) You're looking for one of these two expressions:

(<*>) <$> f <*> v
liftA2 (<*>) f v

And remember in Haskell, variables start with lowercase letters and data constructors start with uppercase letters.

  • Related