MTS 8: LISP and SLIP in MTS
June 1976
and
SEQSL(SRDR,IFLAG)
resemble ADVSER and ADVSEL, respectively. They never terminate on a
name cell; rather, they descend into the sublist. When they arrive at a
terminal sublist (i.e., one that has no more sublists), they assume the
role of the above two linear sequence functions, SEQLR and SEQLL, at
that level. Therefore, IFLAG can only be -1 for datum cells and +1 for
Header cells.
Recursion _________
Recursion is one of the most powerful techniques in list processing.
SLIP also has this facility although its level of elegance is well below
that of LISP.
Let us first consider a few subprograms.
The subroutine
PRESRV(N)
preserves (i.e., pushes down and duplicates) the top cell of the first
N(≤10) public lists, the Ws, which are described in the subsection
"Programming Conventions (SLIP with FORTRAN IV)."
The subroutine
RESTOR(N)
does the opposite. It deletes the top cell from each of the first
N(≤10) public lists.
The function PARMT has two similar forms of implementation. In our
version of SLIP
PARMT2(A,B)
places A and B on the top of the first two public lists, W(1) and W(2)
respectively.
However
PARMT(N,ARG)
puts the first N(≤10) arguments, ARG(1)...ARG(N), in the top cell of the
first N public lists. In both versions of PARMT, the returned value is
the new contents of W(1). If an attempt is made to put values on more
than the allowable public list an error comment is printed and the
program is terminated.
136 SLIP

MTS 8: LISP and SLIP in MTS
June 1976
Now we can return to the problem of recursion. Obviously, if a
FORTRAN routine calls itself before control is yielded back to its
superroutine, the linkages pointing to the superroutine are overwritten
and errors arise. In a similar manner, the values of the arguments to
be transferred are also lost.
If we could, however, store the return locations and the argument
values in push-down stacks, we could solve our problem. Every new call
of a routine would add new top layers to the appropriate stacks
(linkages and argument values), and a return to a superroutine (or
higher-level use of the same routine) would be associated with popping
off the top of the stacks. These push-down and pop-off operations are
performed by the function VISIT and TERM, respectively.
Actually, SLIP provides the facility for using a recursive loop _________ ____
within a subroutine rather than the more general recursive call of
routines. The function VISIT transfers to the first statement of the
loop and provides the arguments for it. The subroutine TERM terminates
the loop and returns control to the last VISIT function executed. It is
possible to go through loops within loops. The format for this is
VISIT(INSNAM)
and
TERM(Y)
where INSNAM is an instruction name to which a statement number was
previously assigned by a FORTRAN ASSIGN statement. Control is trans-
ferred to this statement by VISIT. The return linkage stack, internal
to VISIT, is also pushed down.
The value, Y, delivered by VISIT is actually provided by TERM after
the calculation in the loop is completed.
This action completes VISIT, and control is transferred to the next
statement.
A relatively simple example, using the calculation of N factorial,
gives the best explanation. Using its recursive definition,
N! = N* (N - 1)!
0! = 1
we can code it as follows.
FUNCTION IFACT(N)
COMMON/PUBLICE/ W(100)
DOUBLE PRECISION AVSL,W
DOUBLE PRECISION TOP,NEWTOP,REALL
IF(N.GT.0) GO TO 10
IFACT = 1
RETURN
SLIP 137
Previous Page Next Page