W Wrapl, The Programming Language

Chapter 7


Code in Wrapl can be separated into modules. Each module consists of any number of variable and/or constant declarations, any of which can be exported allowing their use in other modules. To export a variable or constant, follow its declaration with an exclamation mark, !. To use such an exported variable/constant in another module, you can use an import declaration. For example:

1MOD Mod1; 2 3IMP IO.Terminal; 4 5DEF f!(x) ( 6 Terminal.Out:write("x = "); 7 Terminal.Out:write(x); 8 Terminal.Out:write("\n"); 9); 10 11END Mod1.
is a module which exports a single constant, f. In addition, it imports the module IO.Terminal and uses the constant Out exported by that module. If you use the same imported variable/constant repeatedly, you can define the name of that variable/constant in the current scope by using a USE clause.
1MOD Mod1; 2 3IMP IO.Terminal USE Out; 4 5DEF f!(x) ( 6 Out:write("x = "); 7 Out:write(x); 8 Out:write("\n"); 9); 10 11END Mod1.
The full list of modules that are provided with Wrapl can be found in the Libraries Reference.

Imports and Exports

Variables and Constants

In Wrapl, all identifiers must be declared. Variables are declared as VAR name;. This declares a new variable name in the current scope, which will be initialized to NIL. A variable can optionally be initialized when declared, VAR name <- value;.

Variables can be used at any point within the scope they are declared in, even before the actual declarations (i.e. foward declarations are unnecessary). However, the variable is only assigned its initial value at the actual declaration, prior to this it will have the value NIL. For example:

1Out:writes("x = ", x, "\n"); 2VAR x <- 200; 3Out:writes("x = ", x, "\n");
will output
x = NIL x = 200

The alternative syntax VAR name(parameters) body; can be used to initialize a variable to hold a function. For example:

1VAR fact <- <n> ( 2 VAR r <- 1; 3 EVERY r <- r * 1:to(n); 4 RET r; 5);
can be written as
1VAR f(n) ( 2 VAR r <- 1; 3 EVERY r <- r * 1:to(n); 4 RET r; 5);

Constants can be declared in Wrapl using DEF instead of VAR. All constants must be given an initial value when declared, however a constant is assigned its initial value before any variables are initialized or any other expressions are evaluated in the same scope. For example:

1Out:writes("x = ", x, "\n"); 2DEF x <- 200; 3Out:writes("x = ", x, "\n");
will output
x = 200 x = 200