Functions
A function takes inputs and computes a result from them. In L4 each rule is a function: GIVEN names and types its inputs, GIVETH states the type of its result, and DECIDE or MEANS gives the expression that computes it. A function without inputs is a named value.
GIVEN age IS A NUMBER
GIVETH A BOOLEAN
DECIDE `is an adult` age IF age >= 18
Pages
- DECIDE: define a value or a function, including
DECIDE … IFand functions written as several clauses - GIVEN and GIVETH: declare the inputs and the result type
- MEANS: define without
DECIDE - LET and WHERE: local definitions before or after an expression
- YIELD: functions without a name
- AKA: more than one name for the same function, value, type or section
Mixfix functions
A function's name does not have to come first. In a mixfix function, the GIVEN inputs are placed between the words of the name, so a call reads like a sentence:
GIVEN mom IS A STRING
dad IS A STRING
kid IS A STRING
GIVETH A STRING
mom and dad `have a baby named` kid MEANS
CONCAT "mother: ", mom, ", father: ", dad, ", child: ", kid
#EVAL "alice" and "bob" `have a baby named` "carol"
- A call uses the same word order as the definition.
- Inputs can come before, between or after the words, so a function can be infix (
applicant `is entitled to` benefit) or postfix (n squared). - A word of the name can be a quoted identifier with spaces, such as
`is entitled to`, so the words can be legal phrases. - The words that are not inputs act as keywords of that function. When a call mixes inputs and words, L4 finds the function by those words.
An infix function can also be a later operand of a built-in operator such as AND, OR, PLUS or EQUALS:
GIVEN a IS A NUMBER
b IS A NUMBER
GIVETH A BOOLEAN
a `exceeds by at least` b MEANS a AT LEAST b
#EVAL TRUE AND 5 `exceeds by at least` 3
- The call needs no parentheses, because it binds tighter than the infix operators in the precedence table. See Precedence.
- All the words of the call must be on the operand's own line.
- To spread a call over several lines, put it in parentheses and start a new line after the opening parenthesis.
A mixfix function cannot be written as several clauses; use CONSIDER in its body.
When to use mixfix, with worked examples: Make rules read like law.
Related pages
- Types: types of inputs and results
- Operators: operators inside expressions
- Control flow:
IF,BRANCHandCONSIDER