[Openmcl-devel] tracing recursion with ccl

Pascal J. Bourguignon pjb at informatimago.com
Fri Jul 8 12:36:15 PDT 2016


◒ Simon Lucas ◒ <simon at spiral.co.uk> writes:

> CCL's built-in trace does not seem to trace repeated calls to a recursive function call. Not in the way I expected.
>
> Is this correct? I need to get a better idea what is going as I work through examples.  Do I need to write a version of Winston/Horn's our-trace?
>
>
> (defun trim-head (l n)
>   (cond ((zerop n) nil)
>         (t (cons (car l) (trim-head (cdr l) (- n 1))))))
>
> ? (trace trim-head)
> NIL
> ? (trim-head '(a s d f g h j k l) 3)
> 0> Calling (TRIM-HEAD (A S D F G H J K L) 3) 
> <0 TRIM-HEAD returned (A S D)
> (A S D)
> ? 

You can also use cl-stepper which has been written because #+ccl cl:step
is not functional.

; SLIME 2016-02-10
cl-user> (mkupack :use '(:cl-stepper))
#<Package "USER1">
user1> (package-name :cl-stepper)
"COM.INFORMATIMAGO.COMMON-LISP.LISP.STEPPER"
user1> (defun trim-head (l n)
         (cond ((zerop n) nil)
               (t (cons (car l) (trim-head (cdr l) (- n 1))))))


;Compiler warnings :
;   In #1=an anonymous lambda form#2= inside #1##2#trim-head: Undefined function trim-head
trim-head
user1> (trim-head '(a s d f g h j k l) 3)
(a s d)
user1> (step (trim-head '(a s d f g h j k l) 3) :trace)
(Will evaluate (trim-head '(a s d f g h j k l) 3)
 (Entering function trim-head
   (Bind l                to (a s d f g h j k l))
   (Bind n                to 3)
  (Will evaluate (cond ((zerop n) nil) (t (cons # #)))
   (Entering function trim-head
     (Bind l                to (s d f g h j k l))
     (Bind n                to 2)
    (Will evaluate (cond ((zerop n) nil) (t (cons # #)))
     (Entering function trim-head
       (Bind l                to (d f g h j k l))
       (Bind n                to 1)
      (Will evaluate (cond ((zerop n) nil) (t (cons # #)))
       (Entering function trim-head
         (Bind l                to (f g h j k l))
         (Bind n                to 0)
        (Will evaluate (cond ((zerop n) nil) (t (cons # #)))
         Evaluation of (cond ((zerop n) nil) (t (cons # #))) returned one result ==> nil)
        Exiting  function trim-head returned one result ==> nil)
       Evaluation of (cond ((zerop n) nil) (t (cons # #))) returned one result ==> (d))
      Exiting  function trim-head returned one result ==> (d))
     Evaluation of (cond ((zerop n) nil) (t (cons # #))) returned one result ==> (s d))
    Exiting  function trim-head returned one result ==> (s d))
   Evaluation of (cond ((zerop n) nil) (t (cons # #))) returned one result ==> (a s d))
  Exiting  function trim-head returned one result ==> (a s d))
 Evaluation of (trim-head '(a s d f g h j k l) 3) returned one result ==> (a s d))
(a s d)
user1>



-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk




More information about the Openmcl-devel mailing list