Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project!

Re: [Sbcl-help] More question about SBCL regarding tail recursion

 Re: [Sbcl-help] More question about SBCL regarding tail recursion From: Teemu Likonen - 2011-07-13 17:38:34 ```* 2011-06-14T19:53:36+03:00 * Nikodemus Siivola wrote: > On 14 June 2011 19:38, Cheung, Matthew G wrote: >> One thing that has never been clear to me was whether SBCL will by >> default use tail recursion optimisations.  I wrote a general >> "repeated > > Tail-merging is done if (> SPACE DEBUG) or (> SPEED DEBUG) -- not by > default. Is the TRACE output a reliable way to tell whether optimization on recursive calls has occurred? See the following toy example. It is recursive but, if I understand correctly, there is no tail-call. Without much speed optimization: (defun sum (numbers) (declare (optimize (speed 0) (debug 0))) (if numbers (+ (first numbers) (sum (rest numbers))) 0)) CL-USER> (trace sum) (SUM) CL-USER> (sum '(1 2 3 4)) 0: (SUM (1 2 3 4)) 1: (SUM (2 3 4)) 2: (SUM (3 4)) 3: (SUM (4)) 4: (SUM NIL) 4: SUM returned 0 3: SUM returned 4 2: SUM returned 7 1: SUM returned 9 0: SUM returned 10 10 Now the same thing with more speed: (defun sum (numbers) (declare (optimize (speed 3) (debug 0))) (if numbers (+ (first numbers) (sum (rest numbers))) 0)) CL-USER> (sum '(1 2 3 4)) 0: (SUM (1 2 3 4)) 0: SUM returned 10 10 It seems that the was only one call to SUM so either there is some optimization or TRACE couldn't track what's really happening. ```