Created
April 18, 2014 16:52
-
-
Save deanrad/11053843 to your computer and use it in GitHub Desktop.
K-sets clojure explorations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; Anything you type in here will be executed | |
| ;; immediately with the results shown on the | |
| ;; right. | |
| (defn kcomb [k vect] | |
| (cond | |
| (= k 0) '(()) | |
| (empty? vect) '() | |
| :else (concat (map | |
| #(cons (first vect) %) | |
| (kcomb (dec k) (rest vect))) | |
| (kcomb k (rest vect))))) | |
| (kcomb 1 [1 2 3]) | |
| (defn subsets [n items] | |
| (cond | |
| (= n 0) '(()) | |
| (empty? items) '() | |
| :else (concat (map | |
| #(cons (first items) %) | |
| (subsets (dec n) (rest items))) | |
| (subsets n (rest items))))) | |
| (subsets 2 [1 2 3]) | |
| (subsets 1 [1 2 3]) | |
| (kcomb 3 [1 2 3]) | |
| (kcomb 2 [1 2 3]) | |
| (kcomb 1 [1 2 3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment