DECIDE
Defines a named value or function with a body. The primary way to create computations in L4.
Syntax
DECIDE name IS expression
DECIDE name MEANS expression
name MEANS expression
Forms
Named Values
DECIDE pi IS 3.14159
DECIDE greeting IS "Hello, World!"
Functions with GIVEN
GIVEN x IS A NUMBER
DECIDE double x IS x TIMES 2
Using MEANS
MEANS introduces the body of a function:
GIVEN n IS A NUMBER
DECIDE factorial n MEANS
IF n EQUALS 0
THEN 1
ELSE n TIMES factorial (n MINUS 1)
Omitting DECIDE
For functions, DECIDE can be omitted:
GIVEN x IS A NUMBER
double x MEANS x TIMES 2
Examples
Example file:
-- DECIDE keyword examples
-- Simple Named Values
DECIDE `the answer` IS 42
DECIDE `approximate pi` IS 3.14159
DECIDE `welcome greeting` IS "Hello, World!"
DECIDE `is enabled` IS TRUE
#EVAL `the answer`
#EVAL `welcome greeting`
-- Functions with DECIDE and MEANS
-- Simple function using MEANS
GIVEN x IS A NUMBER
`double a number` x MEANS x TIMES 2
#EVAL `double a number` 21
-- Function with DECIDE ... IS
GIVEN n IS A NUMBER
DECIDE `square of` n IS n TIMES n
#EVAL `square of` 7
-- Conditional Decisions
GIVEN age IS A NUMBER
`age category for` age MEANS
IF age < 13 THEN "child"
ELSE IF age < 20 THEN "teenager"
ELSE IF age < 65 THEN "adult"
ELSE "senior"
#EVAL `age category for` 25
#EVAL `age category for` 10
-- Boolean Decisions with IF
ASSUME `person age` IS A NUMBER
ASSUME `has valid ID` IS A BOOLEAN
DECIDE `can enter venue` IF
`person age` >= 21 AND `has valid ID`
#CHECK `can enter venue`
-- Decisions with WHERE Clauses
GIVEN radius IS A NUMBER
DECIDE `area of circle with radius` radius IS
`local pi` TIMES radius TIMES radius
WHERE
`local pi` MEANS 3.14159
#EVAL `area of circle with radius` 10
-- Recursive Decisions
GIVEN num IS A NUMBER
GIVETH A NUMBER
`factorial of` num MEANS
IF num EQUALS 0
THEN 1
ELSE num TIMES `factorial of` (num MINUS 1)
#EVAL `factorial of` 5
-- Multi-parameter Functions
GIVEN a IS A NUMBER
b IS A NUMBER
`maximum of two numbers` a b MEANS IF a > b THEN a ELSE b
#EVAL `maximum of two numbers` 10 25
Simple Values
DECIDE answer IS 42
DECIDE isEnabled IS TRUE
DECIDE message IS "Welcome"
Conditional Decisions
GIVEN age IS A NUMBER
DECIDE isAdult age IS
IF age >= 18
THEN TRUE
ELSE FALSE
With WHERE Clauses
DECIDE circleArea radius IS
pi TIMES radius TIMES radius
WHERE
pi MEANS 3.14159
Boolean Decisions with IF
For boolean-returning functions, use DECIDE ... IF:
GIVEN age IS A NUMBER
GIVEN income IS A NUMBER
DECIDE isEligible IF
age >= 18 AND income > 30000
Related Keywords
- MEANS - Introduces the function body
- GIVEN - Introduces parameters
- GIVETH - Specifies return type
- WHERE - Local definitions
- IF - Boolean condition form
Tutorials
- Using Infix, Postfix, and Mixfix Functions - Define functions that read like natural language