Created
January 11, 2014 04:29
-
-
Save kpmaynard/8367053 to your computer and use it in GitHub Desktop.
Permutations. Specification for permutations. Recursive formula. Let P represent Permutations of a sequence (a1, a2, a3, ... an)
P()=()
P(a1) = (a1)
P(a1 a2) = ((a2 a1) (a1 a2)) <= (map #(weave a2 %) P(a1))
P(a1 a2 a3) = ((a3 a2 a1) (a2 a3 a1) (a2 a1 a3) (a3 a1 a2) (a1 a3 a2) ( a1 a2 a3))... => (map #(weave a3 %) P(a1 a2)) Assume
P(a1, a2,.... a…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn weave | |
([a-seq] | |
(weave (first a-seq) (rest a-seq))) | |
([x a-seq] | |
(defn weave-helper [x a-seq acc n] | |
(println "In weave: Sequence is: " a-seq) | |
(cond (nil? a-seq) (cons x '()) | |
(<= n (count a-seq)) | |
(weave-helper x a-seq (cons (concat (take n a-seq) (cons x (drop n a-seq))) acc) (inc n)) | |
:else acc | |
)) | |
(weave-helper x a-seq '() 0))) | |
(defn permx [a-seq] | |
(if (nil? a-seq) | |
nil | |
(map #(weave (first a-seq) %) (permx (rest a-seq))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment