Skip to content

Instantly share code, notes, and snippets.

@djanatyn
Created September 4, 2012 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djanatyn/3615071 to your computer and use it in GitHub Desktop.
Save djanatyn/3615071 to your computer and use it in GitHub Desktop.
build list of all possible results of flipping coins
(defn split-list [coin-flips]
"returns two lists - one with heads added, and one with tails added"
(let [add-to-front (fn [coll x] (concat coll [x]))]
(list (add-to-front coin-flips 'heads)
(add-to-front coin-flips 'tails))))
(defn list-possible-sequences [num-flips]
(loop [result '((heads) (tails))
count num-flips]
(if (= count 1) result
(recur (apply concat (map split-list result)) (- count 1)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment