OK, cartesian_product it is.

Two comments on your version.  First, unlike mine (and the current
"combinations"), it takes a single argument, a seq of seqs (rather
than multiple seq arguments); which of these ways is preferred?

Second, I had the clauses of my for loop backwards, which was slowing
things down.  I think this version:

(defn combinations "Take a seq of seqs and return a lazy list of
ordered combinations (pick 1 from each seq)"
      [seqs]
      (if (empty? seqs) '(())
        (for [rst (combinations (rest seqs))
              item (first seqs)]
          (cons item rest))))

is actually significantly faster than the iterative one you posted
(and also much simpler); do your benchmarks agree?




On Jan 24, 12:47 am, Mark Engelberg <mark.engelb...@gmail.com> wrote:
> On Fri, Jan 23, 2009 at 11:43 PM,  <jawo...@berkeley.edu> wrote:
> > I think the usual mathematical name is be cartesian-product (or maybe
> > cross-product), although selections is OK with me too.
>
> Yes, now that you've reminded me, I agree that cartesian-product is
> the usual mathematical name, and that would probably be my first
> choice.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to