home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HomeWare 14
/
HOMEWARE14.bin
/
prog
/
proxy14.arj
/
H2.HLP
< prev
next >
Wrap
Text File
|
1992-12-04
|
2KB
|
60 lines
FUNCTION DEFINITION
The syntax of function definition in Proxy is the same as that in C
with minor differences: (1) the declaration of local variables is different
mainly due to the fact that the types of variables are not declared in
Proxy and (2) each function declaration in Proxy is terminated with a
semi-colon, (3) it is not necessary to name the top level function 'main',
(4) procedures do not require the prefix 'void'; in what follows we will
use the generic term function to apply as well to procedures. The syntax
is now shown:
identifier ( op-param-list op-decl) block ;
The definition consists of the function name (identifier) followed by
an optional parameter-list and an optional list of local variables
delimited by parentheses. The parameter-list consists of identifiers
separated by commas. The list of local variables consists of a semi-
colon followed by identifiers separated by commas. The block consists
of a statement-list (see the section on statements) delimited by
curly brackets. Finally, the definition must be terminated with a
semi-colon. Examples follow (the return statement is described in the
section on statements).
even(n) {return n%2==0;};
odd(n) {return !even(n);};
Given a subset of the integers, say digits={0..9};, we can define
a function to obtain the odd integers from this set.
odddigits() {return {n:n<-digits;odd(n)};};
The following example illustrates a simple recursive function to
add up the elements in an integer sequence.
reduce(s) { if(s==[]) return 0; else return hd s + reduce(tl s);};
The next example illustrates the definition of a doubly recursive
function, and the declaration of a local variable x.
quicksort(s;x) { if(len s<2) return s;
x=s[1];
return quicksort([y:y<-s;y<x]) conc [x]
conc quicksort([y:y<-tl s;y>=x]);};
An example showing the use of the existential quantifier:
primes(max) {return [n:n<-[2..max];
!(exists m in {2..n-1};(n % m)==0)];};
An example of a procedure (a function which does not contain a return
statement) which has no parameters.
init() {x["a"]="b";};
This procedure initializes the variable x to the map {"a"->"b"}. The
same effect could have been achieved at the command level by the
assignment x["a"]="b";.