This site has been deprecated. Do not edit pages here. Please visit the new Pico Labs documentation.


Skip to end of metadata
Go to start of metadata

KRL supports functions as first-class objects. KRL supports only anonymous functions, but they can be given names by binding them to a variable in a declaration. Here's an example:

Functions are declared using the keyword function followed by a possibly empty, parentheses-delimited, comma-separated list of parameters and a block of code. The code block of the function is delimited by curly braces and contains a list of semicolon-separated declarations and a single expression. The declarations are optional, but every function code block must end with an expression. The value of the final expression is the return value of the function.

Functions are statically executed (i.e., free variables are bound in the environment in which they are defined, not the environment in which they are executed) and can be recursive. KRE limits the number of times a function can be called in any given ruleset evaluation to protect against runaway code. The value of the recursion threshold is installation specific. Here's an example of a recursive function in KRL:

This example also shows the syntax for function application (e.g., fact(n-1)). An expression resulting in a function is followed by a possibly empty, parentheses-delimited, comma-separated list of expressions. Function application is applicative-order; the expressions representing the function and the arguments are evaluated and the result of executing the function expression (the operator, or rator) is then applied to the results of the evaluation of the arguments (the operands, or rands). Obviously, the rator must be a function.

To see how declarations appear in the body of a function, consider the following example, which uses Newton's method to calculate square roots (Adapted from section 1.1.8 of Structure and Interpretation of Computer Programs.):

Functions can return functions as values and functions can be passed as the arguments to other functions and operators in KRL. The following example defines a generalized summation function called sum that sums the numbers from a to b incrementing using the function inc and applying the function f to each term:

You can use sum to define a function that sums the cubes from a to b:

As another example, you can define a function that creates incrementor functions. When given a number, it returns a function that increments by that value:

Now you use inc_gen to generate other functions:

Labels: