Question

With common lisp and I am assuming the introspection properties. How can I add code to common lisp code that will tell me when a function is called and when has finished executing. I want to take any lisp code and this particular modification to the code. I figure with lisp's AST analysis, this should be possible.

Was it helpful?

Solution

You can use (trace function) for a simple mechanism. For something more involved, here is a good discussion from comp.lang.lisp.

[CL_USER]>
(defun fac (n)
    "Naïve factorial implementation"
    (if (< 1 n)
        (* n (fac (- n 1)))
        1))
FAC
[CL_USER]> (trace fac)
;; Tracing function FAC.
(FAC)
[CL_USER]> (fac 5)
1. Trace: (FAC '5)
2. Trace: (FAC '4)
3. Trace: (FAC '3)
4. Trace: (FAC '2)
5. Trace: (FAC '1)
5. Trace: FAC ==> 1
4. Trace: FAC ==> 2
3. Trace: FAC ==> 6
2. Trace: FAC ==> 24
1. Trace: FAC ==> 120
120
[CL_USER]> 

OTHER TIPS

If CLOS is an option, it has before, after, and around methods that run before, after and around other methods.

Common lisp has a TRACE function that reports the function, arguments and resulting value of each call specified. Here is the doc page for Steel Bank's version, but you should find something similar in most implementations:

http://www.sbcl.org/manual/Function-Tracing.html

The system also includes a profiler:

http://www.sbcl.org/manual/Deterministic-Profiler.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top