Can someone explain to me with simple words what those two operators do:
$
\
Can someone explain to me with simple words what those two operators do:
$
\
\
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
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.
($) :: (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