-6

Please implement the function:

composeApplicative :: (Applicative f) => f (b -> c) -> f (a -> b) -> f (a -> c)

Such that:

(composeApplicative f g) <*> x == f <*> (g <*> x)

Or alternatively, explain why this can not be done?

9
  • 1
    composeApplicative p q = (.) <$> p <*> q
    – isekaijin
    Commented Jul 12, 2015 at 16:27
  • 4
    Is this homework? What have you tried?
    – jub0bs
    Commented Jul 12, 2015 at 16:28
  • 4
    I'm voting to close this question as off-topic because it likely is homework and the OP has shown no effort.
    – jub0bs
    Commented Jul 12, 2015 at 16:37
  • 1
    @Clinton Then would it be so difficult to show what you've tried, and state your problem in a way that expresses that you've encountered a problem, not plainly asking for code. You've had downvotes because you've put so little effort into this question.
    – AJF
    Commented Jul 12, 2015 at 16:46
  • 2
    @Clinton You may say that, but the fact that you've been downvoted indicates that it's a bad idea. Please put effort into a question.
    – AJF
    Commented Jul 12, 2015 at 18:12

1 Answer 1

6

It can be done:

composeApplicative p q = (.) <$> p <*> q

For more information, read the documentation for Applicative functors, more specifically, the composition law. It is effectively a statement that any Applicative instance, composeApplicative f g <*> x must always be equal to f <*> (g <*> x).

As a minor technical note, when doing equational reasoning, the left- and right-hand sides of equations must be separated with a single equals sign (=). The double-equals sign (==) is reserved for decidable runtime equality checks.

2
  • "As a minor technical note, when doing equational reasoning, the left- and right-hand sides of equations must be separated with a single equals sign (=). The double-equals sign (==) is reserved for decidable runtime equality checks." This is not a consensus rule; plenty of high-profile Haskellers use == for equational reasoning. Not even the GHC documentation follows it consistently; see e.g. the docs for Functor and Monad. Commented Jul 13, 2015 at 18:31
  • @LuisCasillas: Point taken. But I think it's useful to distinguish between equality as a proposition (which may or may not be decidable), and decision procedure for equality (which in general doesn't exist for arbitrary types).
    – isekaijin
    Commented Jul 13, 2015 at 19:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.