Tail recursive calls
Hi
I've typed in an BSTree example from "ANSI
Common Lisp" by Paul Graham (page 72). It uses recursion to find place for node
insertion and Lispworks 4.3.7 says that stack is full when I tried to push 10000
values in the tree.
I tried to compile it with all possible OPTIMIZE
options but result was same.
(defun bst-insert (obj bst <)
(declare (optimize (speed 3)(debug 0)(safety 0)))
(do ((cur bst))
((null cur) (make-node :elt obj))
(let ((elt (node-elt bst)))
(if (eql obj elt)
bst
(if (funcall < obj elt)
(make-node
:elt elt
:l (bst-insert obj (node-l bst) <)
:r (node-r bst))
(make-node
:elt elt
:r (bst-insert obj (node-r bst) <)
:l (node-l bst)))
))))
(declare (optimize (speed 3)(debug 0)(safety 0)))
(do ((cur bst))
((null cur) (make-node :elt obj))
(let ((elt (node-elt bst)))
(if (eql obj elt)
bst
(if (funcall < obj elt)
(make-node
:elt elt
:l (bst-insert obj (node-l bst) <)
:r (node-r bst))
(make-node
:elt elt
:r (bst-insert obj (node-r bst) <)
:l (node-l bst)))
))))
Is optimization possible where ? I like recursive
variant more that iterative.
Thanks
Lisper