为什么我不能这样做(翻转(+).digitToInt)$'4'4

Eva*_*oll 6 syntax haskell

我只是想知道它是如何$工作的:我在期待

> (flip (+).digitToInt) $ '4' 4

<interactive>:1:24:
    Couldn't match expected type `t -> Char'
           against inferred type `Char'
    In the second argument of `($)', namely '4' 4
    In the expression: (flip (+) . digitToInt) $ '4' 4
    In the definition of `it': it = (flip (+) . digitToInt) $ '4' 4
Run Code Online (Sandbox Code Playgroud)

适用(flip (+).digitToInt)4 4,但没有奏效.怎么会?我发现这个有效

>  (flip (+).digitToInt) '4' 4
8
it :: Int
Run Code Online (Sandbox Code Playgroud)

而且,我看到的类型:

>  :t (flip (+).digitToInt)
(flip (+).digitToInt) :: Char -> Int -> Int
Run Code Online (Sandbox Code Playgroud)

但是,我不明白为什么我不能(flip (+).digitToInt)明确地申请申请

这种混乱来自于基本的观察

digitToInt $'5'

digitToInt'5'

允许具有相同的效果 - 除了顶部有更多的线路噪音.

sep*_*p2k 15

(flip (+).digitToInt) $ '4' 4
Run Code Online (Sandbox Code Playgroud)

是相同的

(flip (+).digitToInt) $ ('4' 4)
Run Code Online (Sandbox Code Playgroud)

这当然不起作用,因为'4'它不是一个功能.

为了获得你想要的行为,你可以做到

(flip (+).digitToInt $ '4') 4
Run Code Online (Sandbox Code Playgroud)

要不就

(flip (+).digitToInt) '4' 4
Run Code Online (Sandbox Code Playgroud)

  • @Evan:因为$优先级低并且是正确关联的.`foo $ bar $ baz`被解析为`foo $(bar $ baz)`,所以你仍然试图应用''4',好像它是一个函数. (6认同)