2014-09-27 14 views

cevap

12

kolay yolu math.combinatorics kullanıyor:

user> (require '[clojure.math.combinatorics :as combo]) 
nil 
user> (combo/permutations #{"word1" "word2" "word3"}) 
(("word1" "word2" "word3") ("word1" "word3" "word2") ("word2" "word1" "word3") ("word2" "word3" "word1") ("word3" "word1" "word2") ("word3" "word2" "word1")) 

Düzenleme: Ben math.combinatorics uygulamasına bakmadım, ama burada bir tembel sürümü var çünkü OP bazı kodları takip etmeyi istedi. math.combinatorics rağmen

(defn permutations [s] 
    (lazy-seq 
    (if (seq (rest s)) 
    (apply concat (for [x s] 
        (map #(cons x %) (permutations (remove #{x} s))))) 
    [s]))) 
+0

ben "den ISEQ: experiment.core $ eval261 $ fn__262" alıyorum: permütasyon uygulamanızı çalışırken Aşağıda ben takip edebilirsiniz olmayan tembel uygulamasıdır. Olarak adlandırılıyor (println (permütasyonlar # ("w" "d" "m"))). Tembel-dizisi basamıyorum çünkü mi? – slimbo

+1

'# (" w "" d "" m ")', anonim bir işlevin sözdizimidir. # # {"W" "d" "m"} '' @stevemacn –

+0

ughh! Yaptığım hata budur - Teşekkür ederim @diego – slimbo

6

Ben takip etmek daha basit bir şey arıyordum, muhtemelen doğru cevaptır.

(defn permutations [colls] 
    (if (= 1 (count colls)) 
    (list colls) 
    (for [head colls 
      tail (permutations (disj (set colls) head))] 
     (cons head tail)))) 
+0

Tembel sürüm için güncellemeye bakın. –