define
define(s_varg_expression) =>s_vardefine( (s_var[s_formalVar1... ] )g_body... ) =>s_var
Description
(SKILL++ mode only) Is a syntax form used to provide a definition for a global or local variable. The define syntax form has two variations.
Definitions are allowed only at the top-level of a program and at the beginning or within the body of following syntax forms: define (another call to define), lambda, let, letrec, defun, and letseq. If occurring within a body, the define's variable is local to the body.
-
Top Level Definitions
A definition occurring at the top level is equivalent to an assignment statement to a global variable. -
Internal Definitions
A definition that occurs within the body of a syntax form establishes a local variable whose scope is the body. -
define( s_var g_expression )
This is the primary variation. The other variation can be rewritten in this form. The expression is evaluated in enclosing lexical environment and the result is assigned or bound to the variable. -
define( ( s_var [s_formalVar1 ...] ) g_body )
In this variation, body is a sequence of one or more expressions optionally preceded by one or more nested definitions. This form is equivalent to the following define
define( s_var
lambda(( [sformalVar1 ...] ) g_body ...)
Examples
-
First variation
define( x 3 ) => x define( addTwoNumbers lambda( ( x y ) x+y ) ) => addTwoNumbers
-
Second variation
define( ( addTwoNumbers x y ) x+y ) => addTwoNumbers
-
Local definition using second variation
let( (( x 3 )) define( ( add y ) x+y ) ; define add( 5 ) ) ; let => 8
Defines a local functionadd, then invokes it.let( () define( ( f n ) if( n > 0 then n*f(n-1) else 1 ) ; if ) ; define f( 5 ) ) ; let => 120
Declares a single recursive local functionfthat computes the factorial of its argument. Theletexpression returns the factorial of 5.
Related Topics
Function and Program Structure
Return to top