As we write more and more complicated programs, we will want to be able to break the code up into different files that we can load and use as needed. Suppose, for example, that our Gaussian random matrix code is stored in a file called gaussian-matrix.lsp. If we need to use this from an xlispstat session, we just load it in:
> (load "gaussian-matrix")
Lisp uses a require provide syntax.
For example, at the top of the gaussian-matrix lisp
file we would say
while at the top of our new lisp file, the one needing
the gaussian-matrix function, we would put
We can run Lisp-Stat code stand-along by typing
filenameis a lisp file. If
(exit)in it, then xlispstat will exit and return control to the Unix shell when it is finished execuction.
We can also use Lisp-Stat to compile lisp programs.
By default, lisp files are saved with the
file extension. If, from within Lisp-Stat, we were
> (compile-file "filename")the interpreter would look for a file called
filename.lspand compile it into a file called
filename.fsl. So now we could run either the plain lisp or the compiled lisp as a stand alone program by
xlispstat filename xlispstat filename.fsl xlispstat filename.lspThe first one defaults to the compiled version, if it exists.
Here then is a complete example of a lisp program calling another. The first file is the routine to be loaded:
; gaussian-matrix.lsp (provide "gaussian-matrix") (defun gaussian-matrix (l) (let* ((mat (make-array (list l l))) (row (array-dimension mat 0)) (col (array-dimension mat 1)) ) (dotimes (i row) (dotimes (j col) (setf (aref mat i j) (elt (normal-rand 1) 0)) ) ) (copy-array mat) ) )and the next is the ``main'' program which will call this
; random-determinant.lsp ; compute random determinants and dynamically add them to ; a histogram plot (require "gaussian-matrix") (def dim 10) (def myplot (histogram (list (determinant (gaussian-matrix dim))))) (dotimes (i 40) (send myplot :add-points (list (determinant (gaussian-matrix dim) ))) (send myplot :adjust-to-data) (pause 20) ) (exit)
All we have to do to run this is type
and it will run. Try it. It's an example of how Lisp-Stat's object
oriented graphics make dynamical simulations a snap.