previous  next
2.9.7. More Examples of Packages
Here is a simple package for printing lists.

Example

Package $contrib/list

Define About()
  Return "
    Author: Antonio
    Version: 1.0
    Date: 18 July 1997
  "
EndDefine;

Define PrintList(L)
  Foreach X In L Do
    PrintLn X
  EndForeach
EndDefine;

EndPackage;
=================================
Here is another package that takes a pair of objects and turns the
pair into a list.  Note the local alias used to reference the previous
package.

Package $contrib/pair

Alias L := $contrib/list; -- Local alias for another package.
                          -- This alias does not affect global
                          -- aliases.
Define Make(A,B) 
  Return [A,B];
EndDefine;

Define First(P) 
  Return P[1];
EndDefine;

Define Second(P)
  Return P[2];
EndDefine;

Define PrintPairOfLists(P)
  PrintLn 'First list:';
  L.PrintList($.First(P));  -- The local alias, L, is used here,
  PrintLn 'Second list:';
  L.PrintList($.Second(P))  -- and here.  '$' refers to a function
EndDefine;                        -- defined in the current package. 

EndPackage; 
=========================
USING THE PACKAGES.  After reading in the packages using 'Source' or
'<<', one may proceed as follows to use them:

Example

Alias P := $contrib/pair;
X := P.Make([x^2,x],[x,y,z]);
P.PrintPairOfLists(X);
First list:
x^2
x
Second list:
x
y
z
-------------------------------

Note: suppose a package with identifier $contrib/newlist prints lists
in another format.  To switch to this format in $contrib/pair, one
need only change the alias for L from $contrib/list to
$contrib/newlist.