[ create a new paste ] login | about

Link: http://codepad.org/jbDEzu1x    [ raw code | fork ]

Scheme, pasted on Dec 28:
1
2
3
4
5
6
7
8
9
10
11
12
13
; 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)


Create a new paste based on this one


Comments: