In this section, the definitions are stored in files.
You can define function the same way than variable, but using a slightly different syntax.
$ cat docexample/fun_def.txt
square( x ) := x * x;
square( 12 )
$ eq eval -f docexample/fun_def.txt
144
It’s possible to have function with multiple parameters too
$ cat docexample/fun_multi.txt
fun_multi( x, y ) := sum( n = x, 10, y * x );
fun_multi( 8, 2 )
$ eq eval -f docexample/fun_multi.txt
48
You may now want to define functions with many if to change behaviour of your definitions and everything. Sad news, there is no if, no while and no for in Eq Language. But the language provide an other mean to control behaviour, the unification.
We are now going to define a function whose behaviour change in function of the value of it’s parameters.
$ cat docexample/fun_unif1.txt
uniftest( 3, a ) := 2 * a;
uniftest( 1, a ) := a * a;
uniftest( 3, 12 ) * uniftest( 1, 2 )
$ eq eval -f docexample/fun_unif1.txt
96
In the first call uniftest try to match the first argument (here 3) with it’s first pattern (here 3, also, nice). As these two value Match it then continue with the variable a. As a is a variable, it can match with anything at least once. So the first definition of the uniftest match with the given parameters so 2 * a is returned but a is substituted by 12.
For the second call to uniftest We try to match 1 to 3, which can’t work. So we jump to the second definition and by the same principle, 2 * 2 is returned.
The rule for choosing function definition is to take the first definition which match the given argument. So definition order matters.
We now have all the tools necessary to create a if construct by ourselves. By using a specific value to determine which definition of the function to choose, we can write it easily.
$ cat docexample/if_example.txt
-- if( condition (boolean), then, else )
if_( true, a, b ) := a;
if_( false, a, b ) := b;
if_( otherwise, a, b ) := undefined;
if_( 3 > 2, yes, no )
$ eq eval -f docexample/if_example.txt
yes
That was easy. When you find a -- token, the rest of the line is considered as a comment. You can write {- multiline comment like in this sample -}. It’s time to introduce you to some special values :
- true
- false
Which are the Boolean values. All comparisons operators generate a boolean value. So to define an if, it’s a good thing to use them. The if got a third case, which is a bit weird. In a normal utilisation, an if should always use boolean value as first argument (the conditional argument. The reason of this definition need an example :
$ cat docexample/pattern_failure.txt
myfunc( 1 ) := ok;
myfunc( 2 ) := foo;
myfunc( 3 ) := bar;
myfunc( 42 )
$ eq eval -f docexample/pattern_failure.txt; true
No function definition match the parameters
myfunc(42)
#
What you can see here is a pattern matching error. But instead of generating a hard crash, we return the special variable undefined, which can help you to reuse some result later. You can bound the undefined variable like any other variable, it’s just a convention.
Before I told you that the Eq Language didn’t possess an if construct. I have to admit that I lied a bit. There is an if function, defined in the standard library. You can read the standard library (which is rather thin at the moment), but if you modify it the modification won’t be taken into account.