[gambit-list] Efficient Compilation of Tail Calls and Continuations to JavaScript

Marc Feeley feeley at iro.umontreal.ca
Tue Sep 17 15:30:43 EDT 2013


On 2013-09-17, at 3:02 PM, Steve Graham <jsgrahamus at yahoo.com> wrote:

> Found it.  Thanks.
> 
> Why did you define fib twice?

The definition is:

(define (fib n)

  (define (fib n)
    (if (< n 2)
        n
        (+ (fib (- n 1))
           (fib (- n 2)))))

  (fib n))

So there is a global function called fib, in which is defined an internal function called fib.  This allows the compiler to call the internal fib more efficiently, without refering to a global variable.  The semantics of Scheme are such that if the fib global variable is redefined, a recursion on the global variable fib would have to call the new definition.  Such a redefinition is actually what happens when you do (trace fib).  I notice that in the code there is a (declare (block)) which actually makes the internal definition trick needless.

The bottom line is that I did this for performance reasons.

Marc




More information about the Gambit-list mailing list