3

This post and other sources like this insist that the definition of the $-operator is

($) :: (a -> b) -> a -> b
f $ x = f x

or

($) f x = f x

or

($) = id

but I didn't understand why this definition would be able to replace parentheses, so I tried to reproduce that myself and examine the behaviour, by defining:

k :: (a -> b) -> a -> b
k f x = f x

But what I got is:

-- these work:
(+2) `k` 4
(+2) `id` 4
sum `k` [1,2]
sum `id` [1,2]
map (flip(-)3) $ filter even `k`  filter (>=0) [-5..10]
map (flip(-)3) $ filter even `id` filter (>=0) [-5..10]

-- these don't:
sum `k`  1:[2]
sum `id` 1:[2]
map (flip(-)3) `id` filter even $ filter (>=0) [-5..10]

Shouldn't k be a replacement for $? What am I doing wrong?

Lakiryt
  • 33
  • 5

1 Answers1

10

You're missing the fixity declaration:

infixr 0 $

Or in your example:

infixr 0 `k`

Fixity declarations tell the parser what the precedence/associativity of an infix operator is.

melpomene
  • 84,125
  • 8
  • 85
  • 148