tags:

views:

108

answers:

1

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.

+11  A: 
(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
sepp2k
Why is that? Even this doesn't work `(flip (+).digitToInt) $ '4' $ 4` I would expect that to explicitly show the currying effect.
Evan Carroll
actually, that might make sense: this works `((flip (+).digitToInt) $ '4') $ 4`
Evan Carroll
@Evan: Because $ has low precedence and is right associative. `foo $ bar $ baz` is parsed as `foo $ (bar $ baz)`, so you're still trying to apply `'4'` as if it were a function.
sepp2k