In Programming in Haskell by Hutton
In general, if
#
is an operator, then expressions of the form(#)
,(x #)
, and(# y)
for argumentsx
andy
are called sections, whose meaning as functions can be formalised using lambda expressions as follows:(#) = \x -> (\y -> x # y) (x #) = \y -> x # y (# y) = \x -> x # y
What are the difference and relation between "section" and "currying"?
Is a section the result of applying the currying operation to a multi-argument function?
Thanks.
Left sections and right sections are syntactical devices for partially applying an infix operator to a single argument (see also chepner's answer). For the sake of accuracy, we should note that currying is not the same thing as partial application:
Currying is converting a function that takes N arguments into a function that takes a single argument and returns a function that takes N-1 arguments.
Partial application is making a function that takes N-1 arguments out of a function that takes N arguments by supplying one of the arguments.
In Haskell, it happens that everything is curried; all functions take just one argument (even uncurried functions in Haskell take a tuple, which is, strictly speaking, a single argument -- you might want to play with the
curry
anduncurry
functions to see how that works). Still, we very often think informally of functions that return functions as functions of multiple arguments. From that vantage point, a nice consequence of currying by default is that partial application of a function to its first argument becomes trivial: while, for instance,elem
takes a value and a container and tests if the value is an element of the contaier,elem "apple"
takes a container (of strings) and tests if"apple"
is an element of it.As for operators, when we write, for instance...
... we are applying the operator
/
to the arguments5
and2
. The operator can also be used in prefix form, rather than infix:In prefix form, the operator can be partially applied in the usual way:
That, however, arguably looks a little awkward -- after all,
5
here is the numerator, and not the denominator. I'd say left section syntax is easier on the eye in this case:Furthermore, partial application to the second argument is not quite as straightforward to write, requiring a lambda, or
flip
. In the case of operators, a right section can help with that:Note that sections also work with functions made into operators through backtick syntax, so this...
... takes a string and tests whether it can be found in
["apple", "grape", "orange"]
.