Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@moserrya
Last active December 28, 2015 22:59
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 moserrya/7575154 to your computer and use it in GitHub Desktop.
Save moserrya/7575154 to your computer and use it in GitHub Desktop.
4Clojure #97: Pascal's triangle
(fn [string]
(let [br (re-seq #"[()\[\]{}]" string)]
(loop [brackets br acc []]
(if (empty? brackets)
true
(if-let [lb (#{"(" "[" "{"} (first brackets))]
(recur (rest brackets) (conj acc lb))
(if (= (peek acc) ({")" "(" "}" "{" "[" "]"} (first brackets)))
(recur (rest brackets) (pop acc))
false))))))
(defn fact [n]
(loop [n n acc 1]
(if (zero? n)
acc
(recur (dec n) (* n acc)))))
(defn n-choose-k [n k]
(/ (fact n) (* (fact k) (fact (- n k)))))
(defn pascal-row [n]
(for [el (range (inc n))
:let [i (n-choose-k n el)]]
i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment