To better understand how you can make use of the debugger, try working through the following example session. In this example, you define the factorial function, save the definition to a file on disk, compile that file and then call the function erroneously.
A new file is created and displayed in the Editor. If you have not already invoked the Editor, it is started for you automatically.
facto calculate factorial numbers.
(defun fac (n)
(if (= n 1) 1
(* n (fac (- n 1)))))
The Editor switches to the output view while compilation takes place. When prompted, press
to return to the text view. The
function is now defined and available for you to use.
facerroneously with a string argument.
LispWorks notices the error: The arguments of
should be numbers, and one of them is not.
Take a moment to examine the backtrace that is printed in the Backtrace area.
The error displayed in the Condition box informs you that the
function is called with two arguments: the integer 1 and the string "turtle". Clearly, one of the arguments was not the correct type for
, and this has caused entry into the debugger. However, the arguments were passed to
, and so the real problem lies in the
In this case, the solution is to ensure that
generates an appropriate error if it is given an argument which is not an integer.
FACin the Backtrace area of the debugger tool.
The Editor appears. The subform within the definition of
which actually caused the error is highlighted. Double-clicking on a line in the Backtrace area is a shortcut for choosing
Debug > Frame > Find Source
or using the
facfunction so that an extra
ifstatement is placed around the main clause of the function. The definition of
facnow reads as follows:
(defun fac (n)
(if (integerp n)
(if (= n 1) 1
(* n (fac (- n 1))))
(print "Error: argument must be an integer")))
The function now checks that the argument it has been passed is an integer, before proceeding to evaluate the factorial. If an integer has not been passed, an appropriate error message is generated.
fac, once again specifying a string as an argument. Note that the correct error message is generated. You will see it twice, becase
facprints the message and then the Listener prints the return value of
This next part of the example shows you how you can use the various restarts which are listed as commands in the Restarts menu.
facagain with a new argument, but this time type the word
(fac (legnth "turtle"))
You can spot immediately what has gone wrong here, so the simplest strategy is to return a value to use.
You are prompted for a form to be evaluated.
6in the dialog and press OK. This is the value that would have been returned from the correct call to
Having returned the correct value from
is called with the correct argument and returns the value