up previous next
1.9.2 First Example of a Package
The following is an example of a package. It could be typed directly into a CoCoA session, but more normally it is saved into a file to be read into CoCoA using the source command. We will assume that it is stored in a file in the CoCoA directory under the name one.cpkg5.

Example
package $contrib/toypackage

export ToyTest;

define IsNumberOne(n)
  if n = 1 then return true; else return false; endif;
enddefine;

define ToyTest(n)
  if IsNumberOne(n) then
    print "The number 1";
  else
    print "Not the number 1";
  endif;
enddefine;

endpackage; -- of toypackage
Below is output from a CoCoA session in which this package was used:

Example
-- read in the package:
Source "one.cpkg";
/**/ ToyTest(4);  -- was exported
Not the number 1
-- /**/ IsNumberOne(4);  -- !!! ERROR !!! as expected: fn was not exported

/**/ $contrib/toypackage.IsNumberOne(4);
false