Dollar sign inside closed guys

A browser fragment contains an expression that has a dollar sign inside closed partners, for example,

any ($ fst client) 

Since haskellers use the $ sign instead of parens, why are parens needed here?

Why is there a $ character between parens?

I tried to figure out if $ function by doing

 Prelude>:t $ 

But he threw an error, parse error on input $

+5
source share
2 answers

In Haskell, operators are just normal functions that have names consisting of characters and use the default infix. You can use them in the same way as a regular identifier, enclosing them in parentheses:

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

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

 f $ x = fx 

You can get its type in the same way as (+) :

 λ> :t ($) ($) :: (a -> b) -> a -> b 
Haskell operators can also be partially applied as normal functions, wrapping them in parentheses with one-way arguments. For example, (+ 1) matches \ x -> x + 1 , and (1 +) matches \x -> 1 + x .

This also applies to $ , so ($ fst client) matches \ f -> f $ fst client or just \ f -> f (fst client) . The code snippet has checks if any of the function lists returns true given the fst client .

+12
source

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

So, you apply any to a function that takes another function and applies this function to the fst client argument.

+3
source

All Articles