1

I'm trying to learn how the ($) operator works. I run

(+5) ($)  7

I get

 * Non type-variable argument in the constraint: Num (a -> b)
      (Use FlexibleContexts to permit this)
    * When checking the inferred type
        it :: forall a b.
              (Num (a -> b), Num ((a -> b) -> a -> b)) =>
              a -> b

Could anyone help me understand why I get this error ?

bradrn
  • 8,337
  • 2
  • 22
  • 51
Tomer
  • 1,159
  • 7
  • 15

2 Answers2

2

Much like (+) is the prefix form of +.

($), called function application, is the prefix form of $.

> (+) 1 2 == 1 + 2
True

So if you want to apply (+5) to 7 then ($) would do that in this syntax

> ($) (+5) 7
12

which is equivalent to

> (+5) $ 7
12

Note that $ is most often used to simplify syntax.

Mihalis
  • 356
  • 1
  • 2
  • 5
0

I should have done

(+5) $  7

I still not sure I understand the difference.

Tomer
  • 1,159
  • 7
  • 15
  • 6
    The difference is that `(+5) ($) 7` applies the function `(+5)` to the two arguments `($)` and `7` (which makes no sense), whereas `(+5) $ 7` applies the operator `$` to the two arguments `(+5)` and `7` (which does make sense). – bradrn Feb 21 '20 at 04:34
  • When using ($) make sense ? – Tomer Feb 21 '20 at 04:44
  • 2
    `($)` would only make sense if you wanted to use `($)` as a value, for instance if you wanted to pass the function `($)` as an argument to some other function. (Although there aren’t too many times where you would want to do that.) For instance, `fmap ($) [1,2,3]` is equivalent to `[($) 1, ($) 2, ($) 3)]`. **EDITED** – bradrn Feb 21 '20 at 04:47
  • 2
    Actually, now that I think about it, `($)` makes sense in exactly the same situations in which `(any operator)` would make sense. `(+5) ($) 7` makes about as much sense as `5 (+) 7` would. But if you wanted to use the function `($)` as a value — e.g. if you want to pass it as an argument — then you should use `($)` rather than `$`. – bradrn Feb 21 '20 at 04:49
  • 6
    @bradrn `zipWith ($) :: [a -> b] -> [a] -> [b]` is one useful example. – MikaelF Feb 21 '20 at 05:01