Functions
From dis-Emi-A
Contents |
Variable Creation
Full details on assignment can be found in that section. Review:
sqr :> x -> x ^ 2 ;
Creates a function called "sqr" which returns the square of a number.
Note the actual funciton definition is simply:
x -> x ^ 2
Where the assignment is responsible for giving it a name.
Simple Functions
A simple function is functional language like function where a few arguments are passed and a return value is generated. This form allows you to create functions without formally declaring return values or delineating a block of code.
A sample numeric hash function could thus be:
hash :> x -> x mod 15 ;
Or a custom hash function could be:
chash :> x, y -> x mod y
The parameters are always given as a comma separated list to the left, and the return value given to the right.
Complex Functions
Complex functions allow the definition of a return value and its assignment inside a block of code. It allows the definition of more complex functions.
sum :> list -> res
{
res := 0 ;
[ i in list ]
res := res + i;
}
The need for a return statement is eliminated with the named return values. It is of course an error to either use res's value before definition or to complete the function without providing a value for res.
A return statment may nonetheless be used if the function can end earlier.
find :> list, searchval -> index
{
[ ( index, value ) in list ]
[ value == searchval ]
return ;
throw NotFoundError;
}
Multiple Return Values
Multiple return values are handled the same way as multiple parameters.
//remainder and divident
rdiv :> nom, den -> r, div
{
div = floor( nom / den ) ;
r = nom - div ;
}
The short form of the function for multiple return values is also available.
identify :> obj -> typeof( obj ), creator( obj ) ;
Assuming typeof and creator are functions returning something useful.
Lamba and Closure
Lambda and closure expressions become a natural part of the syntax...
TODO
Pass by Copy / Bind
Default is to pass by copy, but sometimes binding is necessary -- to which end must the caller know about it.
TODO
