5

Websocket snippet has a statement that has dollar sign inside closed parens like this,

any ($ fst client)

Since haskellers use $ sign instead of parens, why do we need parens here?

Why is there a $ symbol between parens?

I tried to see if $ is a function by doing

Prelude>:t $

But it threw the error, parse error on input $

Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157

2 Answers2

13

In Haskell, operators are just normal functions that have names made up of symbols and used infix by default. You can use them just like a normal identifier by wrapping them in parentheses:

λ> :t (+)
(+) :: Num a => a -> a -> a

$ is just an operator like this itself. It represents function application and is defined as follows:

f $ x = f x

You can get its type just like (+):

λ> :t ($)
($) :: (a -> b) -> a -> b

Haskell operators can also be partially applied like normal functions, by wrapping them in parentheses with arguments to one side. For example, (+ 1) is the same as \ x -> x + 1 and (1 +) is the same as \x -> 1 + x.

This applies to $ too, so ($ fst client) is the same as \ f -> f $ fst client or just \ f -> f (fst client). The code snippet you have checks if any of a list of functions returns true given fst client.

Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
  • 4
    It might be worth noting that these kinds of partial applications are called sections. – Sarah Apr 17 '15 at 21:42
3

($ fst client) is an operator section (just like (+ 1) or (* 2)) - it partially applies the operator to its right operand. A more verbose way to write it would be (\f -> f $ fst client).

So you're applying any to a function that takes another function and applies that function to the argument fst client.

sepp2k
  • 363,768
  • 54
  • 674
  • 675