Personal tools

Section of an infix operator

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(sectioning examples taken from article Currying)

Latest revision as of 21:40, 1 February 2009

In Haskell there is a special syntax for partial application on infix operators. Essentially, you only give one of the arguments to the infix operator, and it represents a function which intuitively takes an argument and puts it on the "missing" side of the infix operator.

  • (2^)
    (left section) is equivalent to
    (^) 2
    , or more verbosely
    \x -> 2 ^ x
  • (^2)
    (right section) is equivalent to
    flip (^) 2
    , or more verbosely
    \x -> x ^ 2

Like partial application and lambda abstraction, sectioning provides a convenient way of writing some functions without having to explicitly name them:

  • (1+)
    (+) 1
    ) is the "increment" function,
  • (2*)
    is the "double" function,
  • ('\t':)
    is the "indent" function,
  • (`elem` "AEIOU")
    is the "is-capital-vowel-in-English" function (ignoring the "sometimes Y").
Note: as an exception, the "-" (subtraction) operator cannot do a right section, because that would be interpreted as unary negation in Haskell syntax. The Prelude function "subtract" is provided for this purpose. Instead of
(- e)
, you need to write
(subtract e)

[edit] See also