1

Can someone explain to me with simple words what those two operators do:

$

\
Don Stewart
  • 137,316
  • 36
  • 365
  • 468
lknfdhu
  • 479
  • 1
  • 7
  • 12
  • The backslash is a symbol with special meaning in particular contexts. `$` is a function defined in the [prelude](http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-Base.html#%24). – Thomas M. DuBuisson Apr 07 '11 at 21:06
  • See also http://stackoverflow.com/questions/2787543/haskell-what-is-the-difference-between-dollar-and-dollar-exclamation-poi – Don Stewart Apr 24 '11 at 19:36

3 Answers3

15

\ is not an operator, it is part of the literal syntax. More precisely, it is part of two literal syntaxes: it denotes a lambda literal and it serves as an escape character in string literals.

The operator $ is defined in the prelude as

($) :: (a -> b) -> a -> b
f $ x = f x

In other words, it does exactly the same thing as whitespace does, namely just plain function application. However, while function application is left-associative and has high precedence (the highest, in fact), $ is right-associative and has low precedence.

This allows you to omit parentheses when you have chains like "f applied to g applied to h applied to x", which without the $ operator would be written like

f (g (h x))

but with the operator can be written as

f $ g $ h x

It is also useful if you want to pass the function application operator itself as an argument to another function. Say, you have list of functions and a list of values and you want to apply every function in the list to the corresponding value in the other list:

zipWith ($) fs xs
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
7

what those two operators do: $ \

The first one, ($), is an operator, defined as:

-- | Application operator.  This operator is redundant, since ordinary
-- application @(f x)@ means the same as @(f '$' x)@. However, '$' has
-- low, right-associative binding precedence, so it sometimes allows
-- parentheses to be omitted; for example:
--
-- >     f $ g $ h x  =  f (g (h x))
--
-- It is also useful in higher-order situations, such as @'map' ('$' 0) xs@,
-- or @'Data.List.zipWith' ('$') fs xs@.

($)                     :: (a -> b) -> a -> b
f $ x                   =  f x

It allows you to write functions with fewer parenthesese.

The second token, \ is part of the Haskell syntax for lambda abstractions -- anonymous functions.

So, e.g.

\x -> x + 1

is a function that will add 1 to its argument. The syntax for lambda abstractions is described in the Haskell Report.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
2

($) :: (a -> b) -> a -> b base Prelude, base Data.Function Application operator. This operator is redundant, since ordinary application (f x) means the same as (f $ x). However, $ has low, right-associative binding precedence, so it sometimes allows parentheses to be omitted;

keyword \ The backslash "\" is used in multiline strings > "foo\ > \bar" > in lambda functions > > \x -> x + 1

Jake Kalstad
  • 2,045
  • 14
  • 20