[plt-scheme] Re: Scheme efficiency guidelines (or: the fastest way to calc a cartesian product)

From: Albert Neumüller (albert.neu at gmail.com)
Date: Tue Sep 19 15:37:43 EDT 2006


Consider the following code (that I've just seen):

(define (length=2? any)
 (and (cons? any)
      (cons? (rest any))
      (empty? (rest (rest any)))))

I would have written (or rearranged the code of) length=2? like this:

(define (lenght=2? any)
 (and (cons? any)
      (local ((define the-rest (rest any)))
        (and (cons? the-rest)
             (empty? (rest the-rest))))))

This avoids evaluating (rest any) twice.
But who knows - maby setting up a "local define" thus putting the
pointer (or however it is done) into memory (and later garbage
collecting it) is actually less
efficient than just doing the evaluation twice!?

This is the sort of thing I am interested in. Of course it depends on
the scheme interpreter that's used and other factors.
But I suppose on average, there could be some coding guidelines or
conventions - for speed-efficiency, for readability, for

On the otherhand, Scheme probably shines more for its elegance, than
for its speed, so maby most people using Scheme, favour elegent code,
rather than speed -
and I'd rather code the way people in the
Scheme-community code (and know what style is favoured), than try and push
speed-efficiency in a language whose main purpose is not speed-efficieny in the
first place.

Which lenght=2? would you use? How would you write cartesian product?

Any kind comments welcome!

Kind regards,

Posted on the users mailing list.