Function definition: Difference between revisions

Content added Content deleted
Line 2,150: Line 2,150:


=={{header|langur}}==
=={{header|langur}}==
Langur functions are first-order. They are pure in terms of setting values and in terms of I/O (unless declared impure).
A function body may use curly braces, but it is not required if it is a single expression.


A return statement may be used, but a function's last value is its implicit return value.
A return statement may be used, but a function's last value is its implicit return value.


=== parameters ===
Functions defined with explicit parameters may be closures, and those defined with implied parameters are not.
Parameters are defined within parentheses after the fn token. To specify no parameters, use an empty set of parentheses.

<syntaxhighlight lang="langur">val .multiply = fn(.x, .y) { .x * .y }
Langur functions are first-order. They are pure in terms of setting values and in terms of I/O (unless declared impure).

=== explicit parameters ===
Explicit parameters are defined with parentheses after the f token, with no spacing. To specify no parameters, use an empty set of parentheses.
<syntaxhighlight lang="langur">val .multiply = f(.x, .y) .x x .y
.multiply(3, 4)</syntaxhighlight>
.multiply(3, 4)</syntaxhighlight>


=== implied parameters ===
=== curly braces ===
A function body may use curly braces, but it is not required if it is a single expression.
Parameters are implied when the f token is not immediately followed by parentheses without spacing. The implied order of implied parameters is based on the string sort order of their names, not their order within the function.
<syntaxhighlight lang="langur">val .multiply = f .x x .y
<syntaxhighlight lang="langur">val .multiply = fn(.x, .y) .x * .y
.multiply(3, 4)</syntaxhighlight>
.multiply(3, 4)</syntaxhighlight>


=== operator implied functions ===
=== operator implied functions ===
Operator implied functions are built using an infix operator between curly braces on an f token.
Operator implied functions are built using an infix operator between curly braces on an fn token.


<syntaxhighlight lang="langur">val .multiply = f{x}
<syntaxhighlight lang="langur">val .multiply = fn{*}
.multiply(3, 4)</syntaxhighlight>
.multiply(3, 4)</syntaxhighlight>


=== nil left partially implied functions ===
=== nil left partially implied functions ===
These are built with an infix operator and one operand inside the f{...} tokens.
These are built with an infix operator and a right-hand operand inside the fn{...} tokens.


<syntaxhighlight lang="langur">val .times3 = f{x 3}
<syntaxhighlight lang="langur">val .times3 = fn{* 3}
map .times3, [1, 2, 3]</syntaxhighlight>
map .times3, [1, 2, 3]</syntaxhighlight>


=== impure functions (I/O) ===
=== impure functions (I/O) ===
Impure functions must be declared as such.
Impure functions must be declared as such.
<syntaxhighlight>val .writeit = impure f(.x) { writeln .x }</syntaxhighlight>
<syntaxhighlight>val .writeit = impure fn(.x) { writeln .x }</syntaxhighlight>


Impure functions cannot be passed to pure functions.
Impure functions cannot be passed to pure functions.