Up: Index.
Previous: Introduction.
Next: Menus.
Language
Lambda expressions are built based on four simple rules.
- A single identifier is a lambda expression. Lambda
Calculator will treat any string of non-space characters
as an identifier, as long as it does not contain a backslash,
period, parenthesis, bracket, or brace.
- A function can be applied to an argument by writing a
lambda expression representing the function and then another lambda
expression representing the argument.
The expression "fact 4" would represent a call to the
fact function passing 4 as its parameter.
Note that parentheses are not necessary surrounding the function
argument.
- A lambda expression can be abstracted by writing a
lambda (represented with a backslash in Lambda Calculator),
then an identifier, then a period, then the lambda expression.
This represents a function whose parameter is named as in the identifier
and whose return value is the value computed by the lambda expression.
For example, "λx.x" represents the identity function.
- Finally, you can give parentheses to clarify precedence.
You can alternatively use brackets ('[]') or braces ('{}');
these alternatives can be useful when you have a long expression and
have a difficult time matching parentheses.
Without parentheses, a function application has a higher order of
precedence than function abstraction, and function applications are
left-associative. For example, the expression
"λx.f g x"
would represent a function that takes in an argument x and
returns the
result of applying g to f, then x to the
function that this returns.
If you want instead to pass x to g and the result to
f, you would need parentheses:
"λx.f (g x)". This latter expression would represent a
function the functional composition of f and g.
Thus, when we consider the lambda expression
(λx.* x x) (- 3 1)
we see that Lambda Calculator will understand this
as the application of the argument (- 3 1) (whose value is 2)
into the function taking x as a parameter and returning the
value of * x x. The value of * 2 2, and hence of this
lambda expression, is 4.
The following lambda expression is a more difficult one.
(λf.λz.f (f z)) (λx.* x x) (- 3 1)
The first parenthesized term represents a function that takes f
as a parameter and returns a function taking z as a parameter
and returning the value of f (f z).
In this case, we pass the function "λx.* x x" into this
function as f, and so we get the function taking z as
a parameter and returning the value of
(λx.* x x) ((λx.* x x) z)
Into this function we pass the value of (- 3 1), which is 2.
Simplifying our expression, we get
(λx.* x x) (* 2 2)
(λx.* x x) 4
* 4 4
Thus, we arrive at 16. (Note, however, that Lambda Calculator
would simplify the expression in a different order.)
The applied calculus
When the "Use Applied Calculus" option is selected (as it is by
default), the engine
includes a variety of computational functions already.
+, -, *, / |
take two numerical identifiers and return an identifier
representing the binary operation applied to them. |
<, >, <=, >=,
=, /= |
take two numerical identifiers and return the identifier
true or false representing how they compare.
(The /= function represents inequality.) |
if |
take three values, the first being the identifier
true or false, returning the second value
if the identifier is true and the third if it is
false. |
Note: If you have selected the applicative
evaluation, the built-in if function will not necessarily
compute as you might expect. The engine will attempt to evaluate all
three arguments first before applying the if function,
which could lead to unnecessary infinite computation.
Up: Index.
Previous: Introduction.
Next: Menus.