[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 10:50:51 EDT 2006

Hello again!

(cartesian-product1c can be further optimized (or not?) as is shown in
cartesian-product1d below)

Concerning cartesian-product1c: Instead of passing x-single as a
parameter the whole time, we create a local expression:

;;cartesian-product1d : (listof SV) -> (listof onepair)
(define (cartesian-product1d alist)
  (local ((define (do-cartesian1d x-single x-rest)
            (local ((define (cartesian-with-fixed-x1d y)
                        [(empty? y) (cond
                                      [(empty? x-rest) empty]
                                      [else (do-cartesian1d (first
x-rest) (rest x-rest))])]
                        [else (cons (make-onepair x-single (first y))
                                    (cartesian-with-fixed-x1d (rest y)))])))
              (cartesian-with-fixed-x1d alist))))
      [(empty? alist) empty]
      [else (do-cartesian1d (first alist) (rest alist))])))

Kind regards,
Albert Neumüller

Posted on the users mailing list.