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

### 5.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 5.39.10).

The sommet command finds the top of an expression.

- sommet takes one argument:
  expr, an expression.
- sommet(expr) returns the top of expr.

#### Examples.

- Input:
  sommet(sin(x+2))
  Output:

  sin

- Input:
  sommet(x+2*y)
  Output:

  +

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

- op takes one argument:
  expr, an expression.
- op(expr) returns the leaves of expr.

#### Examples.

- Input:
  op(sin(x+2))
  or:
  feuille(sin(x+2))
  Output:

  x+2

- Input:
  op(x+2*y)
  or:
  feuille(x+2*y)
  Output:

  x,2 y

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 5.3.4, Section 5.37.1, Section 5.38.2, Section 5.40.6, Section 5.55.4 and Section 5.55.5 for specific uses of left and right.)

- left and right take one argument:
  expr, an expression whose top is an infixed operator.
- left(expr) returns the left side of the operator.
- right(expr) returns the right side of the operator.

#### Examples.

- Input:
  sommet(y=x^2)
  Output:

  =

- Input:
  left(y=x^2)
  Output:

  y

- Input:
  right(y=x^2)
  Output:

  x2

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);
}