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
> (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.
In butterfly and breaststroke races, regulations require swimmers to touch the end of the pool with both hands simultaneously before turning back for another length. While they legally can flip turn during butterfly and breaststroke races, it is more common to turn left or right to begin the next lap.
(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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With