How SYMMETRICA does it More details Factorial numbers The first object

The first object

In order to initialize certain things (like ''global data structures``, but we need not explain this in detail here), and afterwards to close them in order to free the corresponding memory, we need now to remind you of the following two function calls which you have already seen:
anfang(), ende()
(the German word for beginning and for end) in between these we have to write our programs. Therefore the smallest example of a main() routine inside SYMMETRICA is the following:
main()
{
anfang();
ende();
}
This routine is of course not very exciting, since just nothing happens if it is carried out.

To begin with the object oriented way of evaluation of n! we have to define a function that associates with an object called input an object called output in such a way that if input is the object arising from the number n, the output object is the object corresponding to the desired number obfak(n). We call this function that hopefully does what we want

obfak(input,output)
Since we have to tell the computer that input and output are meant to be objects, the definition will start as follows:
obfak(input,output)
OP  input, output;
{
}
Note that OP is used in order to say that the following things are meant to be objects (a precise definition of object is postponed to later sections).

Then we have to introduce further variable objects which we shall need later on in the recursion, let us call them h1,h2. We have to reserve storage space for these variables which will be objects, and therefore we include a line OP h1,h1; Hence the first lines of the program now look as follows:

obfak(input,output)
OP  input, output;
{
OP  h1,h2;
}
In order now to program the recursion we have to begin with the starting values obfak(n), for n=1. In order to do this we have first of all to say that if input is equal to the object that corresponds to the number 1, then the function obfak takes as its value the object that corresponds to the number 1. The check is done by
einsp
and
m_i_i
allows to build an object from a number. Hence the desired routine looks as follows:
obfak(input,output)
OP  input, output;
{
OP  h1,h2;
if  (einsp(input))
return(m_i_i(1L,output));
}
Now we are going to program the recursion formula where the variable objects h1,h2 will occur, and so we have to begin with reserving storage space for these variables (which is done by callocobject(); at a suitable place of the program), as well as to define the starting values:
obfak(input,output)\\
OP  input, output;
{
OP  h1,h2;
if  (einsp(input))
return(m_i_i(1L,output));
h1=callocobject();
copy(input,h1);
dec(h1);
h2=callocobject();
obfak(h1,h2);
mult(h2,input,output);
freeall(h1);
freeall(h2);
}
(The command copy explains itself, and the command dec abbreviates decrease.) The final step is to type an integer n into the computer and get the corresponding factorial number of square roots in return. This can be done by an application of scan and println in the following way:
#include "def.h"
#include "macro.h"
main()
{
OP  a,b;
anfang();
a= callocobject();
b= callocobject();
scan(INTEGER,a);obfak(a,b);println(b);
freeall(a);freeall(b);
ende();
}
obfak(input,output)
OP  input, output;
{
OP  h1,h2;
if  (einsp(input))
   return(m_i_i(1L,output));
h1=callocobject();
copy(input,h1);
dec(h1);
h2=callocobject();
inc(h1);
obfak(h1,h2);
mult(h2,input,output);
freeall(h1);
freeall(h2);
}
This is the complete program (it can be found in ex8.c), and you can now start using it by simply replacing test.c by ex8.c and then typing
make
After a short while, if make is executed successfully, then you type
a.out
to run the program. You will then be asked for an integer. You will find on your screen the following prompt:
integerobject
You answer this question by typing, say 10 an then you press the return key. After a very short time you will get the reply 3.628800 (Note that long integers are broken up into groups of 6, seperated by a dot.) You can rerun the program typing a.out again and, if you are brave, enter the integer 20. The reply will be 2.432902.008176.640000 which is in fact 20! (in terms of D. E. Knuth: Oh what a glorious day!).
harald.fripertinger "at" uni-graz.at, May 26, 2011

How SYMMETRICA does it More details Factorial numbers Uni-Graz Mathematik The first object Valid HTML 4.0 Transitional Valid CSS!