[Openmcl-devel] consing

Joshua TAYLOR joshuaaaron at gmail.com
Wed May 15 18:12:38 UTC 2013


On Tue, May 14, 2013 at 10:10 AM, John Miller <millejoh at me.com> wrote:
> Ummm, tail recursion?
>
> (dissasemble 'fac) might make for an interesting read.

CCL doesn't optimize the non-tail recursive

(defun fac (n) (if (= n 0) 1 (* n (fac (- n 1)))))

into a tail-recursive version with an accumulator, though, at the
default optimization levels, anyway.  Rather it performs the recursive
call (fac (- n 1)) and then returns to multiply the result by n:

$ ccl64
Welcome to Clozure Common Lisp Version 1.7-r14925M  (LinuxX8664)!
? (defun fac (n) (if (= n 0) 1 (* n (fac (- n 1)))))
FAC
? (compile 'fac)
FAC
NIL
NIL
? (disassemble 'fac)
;;; (defun fac (n) (if (= n 0) 1 (* n (fac (- n 1)))))
L0
...<snip>
;;; (fac (- n 1))
L124
   [124] (movl ($ 8) (% nargs))
   [129] (nop)
   [133] (nop)
   [136] (callq L7)
   [141] (leaq (@ (:^ L0) (% rip)) (% fn))

;;; (* n (fac (- n 1)))
   [148] (movq (% save0) (% arg_y))
   [151] (nop)
   [155] (nop)
   [158] (callq (@ .SPBUILTIN-TIMES))
   [165] (leaq (@ (:^ L0) (% rip)) (% fn))
...<snip>



-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/



More information about the Openmcl-devel mailing list