Skip to content

Instantly share code, notes, and snippets.

@maxcountryman
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxcountryman/9058576 to your computer and use it in GitHub Desktop.
Save maxcountryman/9058576 to your computer and use it in GitHub Desktop.
(declare permutations)
(defn permute [s x]
(let [ps (permutations (remove (partial = x) s))]
(map #(cons x %) ps)))
(defn permutations [s]
(if (-> s (count) (= 1))
(list s)
(mapcat (partial permute s) s)))
;; => (permutations "abc")
;; ((\a \b \c) (\a \c \b) (\b \a \c) (\b \c \a) (\c \a \b) (\c \b \a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment