// file list-tools.choc
// author : E. Duris
// date : 24.04.97
// last update : 20.05.97
class List {
// Constructor (production) declarations
case Cons(int, List);
case Nil ();
// Attributes (methods) declarations
// REVERSE computes the reversed list with an accumulating parameter
List reverse
{
List acc;
List result;
case Cons (a, l) :
l.acc = Cons(a,this.acc);
this.result = l.result;
case Nil ():
this.result = this.acc
case reverse :
this.acc = Nil;
return this.result;
} // end reverse
// APPEND concatenates the parameter list at the end of the current list.
List append (List)
{
List hy;
List result;
case Cons(a, l) :
this.result = Cons(a,l.result);
l.hy = this.hy;
case Nil ():
this.result = this.hy;
case append (y) :
this.hy = y;
return this.result;
} // end append
// LENGTH computes the number of elements of the current list.
int length ()
{
int result;
case Cons(a, l) :
this.result = 1 + l.result;
case Nil ():
this.result = 0;
case length () :
return this.result;
} // end length
// SUM computes the sum of the elements of the current list.
int sum ()
{
int result;
case Cons(a, l) :
this.result = a + l.result;
case Nil ():
this.result = 0;
case sum () :
return this.result;
} // end sum
// AVERAGE concatenates the parameter list at the end of the current list.
float average ()
{
if this.length == 0 return 0; //
default value
return this.sum / this.length;
} // end average
} // end class List