codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
; permutations without repetitions (r choices from n) (defmacro permutations [n r] (let [locals (take r (repeatedly gensym)) for-range (apply concat (for [i (range r)] [(nth locals i) `(range ~n)]))] `(for [~@for-range :when (= ~r (count (distinct [~@locals])))] [~@locals]))) ; permutate a vector without repetitions (r choices from vector v) (defn permutate [v r] (let [perms (permutations (count v) r) map-perm (fn [perm] (map #(nth v %) perm))] (map map-perm perms))) (permutate [10 20 30 40 50] 3) ;spits java.lang.ClassCastException: clojure.lang.Symbol cannot be cast to java.lang.Number (NO_SOURCE_FILE:229)
Private
[
?
]
Run code
Submit