chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] How to structure a project


From: felix winkelmann
Subject: Re: [Chicken-users] How to structure a project
Date: Thu, 11 Jan 2007 07:42:53 +0100

On 1/10/07, Graham Fawcett <address@hidden> wrote:
On 1/10/07, Zbigniew <address@hidden> wrote:
> Apropos of nothing, if you surround the body of FAC with (let ((fac
> fac)) ...) or even (let loop ((n n)) ...) then you avoid a global
> lookup on FAC on every recursive call.  This is the same as compiling
> the file in block mode, except the latter doesn't work for exported
> definitions.  This is something most people here probably know, but
> it's an interesting optimization trick that took me a while to come
> across.

Thanks, Zbigniew -- I had never thought to do that. :-)
A macro comes to mind:

(define-macro (define/rec sig . body)
  (let ((name (car sig)))
    `(define (,name ,@(cdr sig))
       (let ((,name ,name))
         ,@body))))

(macroexpand '(define/rec (fac n) (if (zero? n) 1 (* n (fac (- n 1))))))
=> (##core#set! fac (lambda (n) (let ((fac fac)) (if (zero? n) 1 (* n
(fac (- n 1)))))))

The historical MacScheme compiler (and possibly Larceny) actually
have an optimization option ("benchmark-mode") that makes this
the default behaviour of "define".


cheers,
felix




reply via email to

[Prev in Thread] Current Thread [Next in Thread]