Haskell 的 $-运算符的定义

Definition of Haskell's $-Operator

这个post and other sources like this坚持$-operator的定义是

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

($) f x = f x

($) = id

但我不明白为什么这个定义能够替换括号,所以我尝试自己重现并检查行为,通过定义:

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

但是我得到的是:

-- 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]

k 不应该替代 $ 吗?我做错了什么?

你错过了 fixity declaration:

infixr 0 $

或者在你的例子中:

infixr 0 `k`

固定性声明告诉解析器中缀运算符的 precedence/associativity 是什么。