up previous next
2.7.2 FUNCTIONs are first class objects
In CoCoA-5 functions are "first class objects", and so may be passed like any other value.

-- The following function MyMax takes a function LessThan as parameter,
-- and returns the maximum of X and Y w.r.t. the ordering defined by the
-- function LessThan.

/**/  Define MyMax(LessThan, X, Y)
/**/    If LessThan(X, Y) Then Return Y; Else Return X; EndIf;
/**/  EndDefine;

-- Let's use MyMax by giving two different orderings.

/**/  Define CompareLT(X, Y)  Return LT(X) < LT(Y);  EndDefine;
/**/  Define CompareLC(X, Y)  Return LC(X) < LC(Y);  EndDefine;

/**/  use R ::= QQ[x,y,z];
/**/  MyMax(CompareLC, 3*x-y, 5*z-2);
5*z -2
/**/  MyMax(CompareLT, 3*x-y, 5*z-2);
3*x -y