Nice! You're right that fold-right is more expensive than fold, but I didn't think it'd matter much since the argument list is expected to be pretty short. Anyway, nice to have this figured out.

Best regards,
Kevin Wortman


On Fri, Aug 22, 2014 at 1:26 PM, John Cowan <xxxxxx@mercury.ccil.org> wrote:
Kevin Wortman scripsit:

> (define (cartesian . sets)
>   (fold-right (lambda (set tuples)
>         (append-map! (lambda (element)
>                    (map (cute cons element <>) tuples))
>                  set))
>           '(())
>           sets))

This is the same algo that I worked out walking to work today, except
that I reversed "sets" in advance (it's safe to use reverse!) and did
a left fold, because left folds are tail-recursive in eager languages.

--
John Cowan          http://www.ccil.org/~cowan        xxxxxx@ccil.org
You know, you haven't stopped talking since I came here. You must
have been vaccinated with a phonograph needle.
        --Rufus T. Firefly