hgiesel
hgiesel

Reputation: 5648

How can (<$>) be left associative

I just noticed that (<$>) has a fixity of infixl 4. How can this be?

(+1) <$> (/5) <$> [5,10] obviously works right to left.

Upvotes: 16

Views: 729

Answers (1)

Cubic
Cubic

Reputation: 15683

No, <$> is left associative and this is no different in your example. (+1) <$> (/5) <$> [5,10] is read as ((+1) <$> (/5)) <$> [5,10]. This happens to work because of the Functor instance of (->) a is basically equivalent to function composition; fmap (+1) (/5) is equivalent to \x -> (x/5)+1, which in this instance gives you the same result as what you'd get with the order you appear to think this works in, i.e. (+1) <$> ((+5) <$> [5,10]).

Because this is a little bit confusing, if you want to apply multiple functions in a row it's probably better for readability to use the normal function composition operator here: (+1) . (/5) <$> [5,10].

Upvotes: 18

Related Questions