Skip to content

Instantly share code, notes, and snippets.

@deanrad
Created April 18, 2014 16:52
Show Gist options
  • Select an option

  • Save deanrad/11053843 to your computer and use it in GitHub Desktop.

Select an option

Save deanrad/11053843 to your computer and use it in GitHub Desktop.
K-sets clojure explorations
;; 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