Tom's Lisp provides only the most primitive facilities for printing. Tom's Lisp programs can print only to standard output; there is no facility to open files. There is no input facility under program control, either.
The two operations it does have are print and sprint. The print primitive takes a single object and outputs an external representation of the object. It then evaluates to its argument. This operation calls the same print function which the interpreter itself uses to print the values of the expressions you enter.
Simply running the print from the command prompt shows the value twice, since print displays the argument, evaluates to the argument, then the interpreter again prints this value. The print function is more useful when called inside another function. We'll see one in a moment.
First, though, we also need the sprint function. Its name stands for “string print” since it takes a string and prints out the characters it contains, without the surrounding quotes. Since the input parser knows obeys the \n and \t escape sequences when it reads strings, these are useful with sprint.
This function is also fairly pointless when entered directly, but it can be useful inside functions. For instance:
While we could certainly create a count function which takes two integers and returns a list of the count from one to the other, that would not let us control how the system prints it. With print and sprint, we can format the list as we like.
For a more complex example, suppose we wish to work with polynomials as lists of coefficients. You can evaluate a polynomial at x quite easily:
A function to print the polynomials in a readable form would go nicely with our little evaluator.
The prpoly-r function goes recursively through the terms, printing a representation of each one. It skips zero terms, suppresses exponents below two, and prints the constant term as a constant. Its parameter first means that the function hasn't printed anything yet, and is used to suppress printing a leading plus sign. The prpoly function is a wrapper which just handles the case of the empty list, and polyat produces a nice line of output by calls to print and sprint, as well as prpoly. It does play one dirty trick by allowing its own value (which comes from polyval) to become part of the output line. This makes the function value printed by the interpreter seem to disappear.