# Difference between revisions of "Section of an infix operator"

(Category:Glossary) |
m |
||

(3 intermediate revisions by 3 users not shown) | |||

Line 1: | Line 1: | ||

− | In Haskell there is a special syntax for [[partial application]] on [[infix operator]]s. | + | In Haskell there is a special syntax for [[partial application]] on [[infix operator]]s. 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. |

− | * <hask>(2^)</hask> is equivalent to <hask>(^) 2</hask> | + | * <hask>(2^)</hask> (left section) is equivalent to <hask>(^) 2</hask>, or more verbosely <hask>\x -> 2 ^ x</hask> |

− | * <hask>(^2)</hask> is equivalent to <hask>flip (^) 2</hask> | + | * <hask>(^2)</hask> (right section) is equivalent to <hask>flip (^) 2</hask>, or more verbosely <hask>\x -> x ^ 2</hask> |

+ | |||

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

+ | * <hask>(1+)</hask> (unsugared: <hask>(+) 1</hask>) is the "increment" function, | ||

+ | * <hask>(2*)</hask> is the "double" function, | ||

+ | * <hask>('\t':)</hask> is the "indent" function, | ||

+ | * <hask>(`elem` "AEIOU")</hask> 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 <hask>(- e)</hask>, you need to write <hask>(subtract e)</hask>. | ||

+ | <br> | ||

+ | Note: Iterated sections are also possible, as long the associativity is correct: <hask>(1+2+)</hask>. The famous (but mostly useless) "Bender" operator is <hask>(:8:[])</hask>. | ||

+ | |||

+ | == See also == | ||

+ | |||

+ | * [[Currying]] | ||

+ | * [http://www.haskell.org/onlinereport/exps.html#sections Haskell report: Sections] - see for more details | ||

[[Category:Glossary]] | [[Category:Glossary]] | ||

[[Category:Syntax]] | [[Category:Syntax]] |

## Latest revision as of 17:58, 31 March 2017

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+)`

(unsugared:`(+) 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)`

.

Note: Iterated sections are also possible, as long the associativity is correct: `(1+2+)`

. The famous (but mostly useless) "Bender" operator is `(:8:[])`

.

## See also

- Currying
- Haskell report: Sections - see for more details