Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn drop-nth
- "Removes the nth element from a collection."
- [sqn n]
- (concat (take n sqn) (drop (inc n) sqn)))
- (defn drop-each
- "Produces a collection of copies of the input, each with a different element removed."
- [sqn]
- (map #(drop-nth sqn %)
- (range (count sqn))))
- (defn cons-to-all
- "Exactly what it says on the tin."
- [x, sqns]
- (map #(cons x %) sqns))
- (defn permutations
- "Produces a collection of all possible unique rearrangements of the input sequence."
- ;; FINALLY.
- [sqn]
- (if (= 1 (count sqn))
- #{sqn}
- (reduce into #{}
- (map #(cons-to-all %1 (permutations %2))
- sqn
- (drop-each sqn)))))
- ==================== interactive output ====================
- mathdice-solver.core=> (permutations [1 2 3])
- #{(2 3 1) (1 3 2) (2 1 3) (3 1 2) (1 2 3) (3 2 1)}
- mathdice-solver.core=> (permutations [1 2 2])
- #{(2 2 1) (2 1 2) (1 2 2)}
- mathdice-solver.core=> (permutations [1 1 1])
- #{(1 1 1)}
Advertisement
Add Comment
Please, Sign In to add comment