Previous Up Next

6.15.3  Top and leaves of an expression: sommet feuille op left right

An expression can be represented by a tree. The top of the tree is either an operator or a function and the leaves of the tree are the arguments of the operator or function (see also 6.39.10).

The sommet command finds the top of an expression.


Examples.


The op command finds the list of the leaves of an expression.
feuille is a synonym for op.


Examples.


If the top of an expression expr is an infixed operator, the left hand side will be expr[1] and the right hand side will be expr[2]. The left and right commands are alternative commands to find the sides (see Section 6.3.4, Section 6.37.1, Section 6.38.2, Section 6.40.6, Section 6.55.4 and Section 6.55.5 for specific uses of left and right.)


Examples.


Remark.
If a function is defined by a program (see Section 12.1.2) then the top will be the function ’program’ and the leaves will be a sequence consisting of the arguments of the defined function, followed by a sequence of 0s (one for each argument) followed by the body of the function. For example, define the pgcd function:

pgcd(a,b):={local r; while (b!=0) {r:=irem(a,b);a:=b;b:=r;} return a;}

Then:
Input:

sommet(pgcd)

Output:

program

Input:

feuille(pgcd)[0]

or:

op(pgcd)[0]

Output:

a,b

Input:

feuille(pgcd)[1]

or:

op(pgcd)[1]

Output:

0,0

Input:

feuille(pgcd)[2]

or:

op(pgcd)[2]

Output:

   { local r;
    while(b<>0){
    r:=irem(a,b);
    a:=b;
    b:=r;
    };; ;
    return(a);
   }

Previous Up Next