//
you're reading...
Uncategorized

Clojure Codes for Generating Combinatorial Objects

(defn k-comb [ k items ]
  (cond
     (= k 0) (list ())
     (empty? items) '()
     :else
      (let [f    (first items) 
            k1   (k-comb (dec k) (rest items))
            f-in (map #(conj % f) k1)
            f-out   (k-comb k (rest items))]  
        (into f-in f-out))))

;;user>(k-comb 3 (range 5))
;;((1 2 3) (1 2 4) (1 3 4) (2 3 4) (0 2 3) (0 2 4) (0 3 4) (0 1 3) (0 1 4) (0 1 2))
Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: