So I recently decided to start learning Clojure.  I installed Clojure
box and wrote this little method to compute all possible combinations
of input:
 (defn combinations [items]
        (if (== (count items) 1)
            (flatten items)
            (for [frstitems (flatten (first items))
                  tlitm (combinations (rest items))]
                  (flatten (list frstitems tlitm)))))

so (combinations [[1 2] [1 2]])

returns

((1 1) (1 2) (2 1) (2 2))

Is there a way I can get rid of the if form?  Having the if statement
duplicates what the for loop does when it creates frstitems.  I tried
removing the if statement so the function looks like:

 (defn combinations [items]
            (for [frstitems (flatten (first items))
                  tlitm (combinations (rest items))]
                  (flatten (list frstitems tlitm))))

but when I do this the function just returns an empty list.  I tried
to figure out why using the REPL but did not discover the problem.

Is what I'm asking possible and if so what would the function look
like?   Thank you.

So far Clojure is pretty cool!

Mike

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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