开发者

Why can't I do (flip (+).digitToInt) $ '4' 4

开发者 https://www.devze.com 2023-01-21 13:55 出处:网络
I\'m just wondering how $ works: I was expecting > (flip (+).digitToInt) $ \'4\' 4 <interactive>:1:24:

I'm just wondering how $ works: I was expecting

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

to apply (flip (+).digitToInt) to 4 4, however it didn't work. How come? I've found this works

&开发者_JAVA技巧gt;  (flip (+).digitToInt) '4' 4
8
it :: Int

And, I see the type of:

>  :t (flip (+).digitToInt)
(flip (+).digitToInt) :: Char -> Int -> Int

But, I don't understand why I can't call apply (flip (+).digitToInt) explicitly

This confusion comes from the basic observation that

digitToInt $ '5'

and

digitToInt '5'

are permitted with the same effect - except that the top has slightly more line noise.


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

is the same as

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

Which of course does not work because '4' is not a function.

To get the behavior you want, you can do

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

Or just

(flip (+).digitToInt) '4' 4
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号