Skip to content

Instantly share code, notes, and snippets.

@Jach
Created December 8, 2011 18:58
Show Gist options
  • Save Jach/1448054 to your computer and use it in GitHub Desktop.
Save Jach/1448054 to your computer and use it in GitHub Desktop.
Stuff from LOL translated into Clojure
; listing 1.4: group
(defn group [coll n]
(if (zero? n)
(throw (Exception. "zero length"))
(partition-all n coll)))
; listing 1.5: flatten
; built-in
; listing 1.6: fact-and-choose
(defn fact [x]
(loop [n x total 1]
(if (zero? n)
total
(recur (dec n) (* total n)))))
(defn choose [n r]
(/ (fact n)
(fact (- n r))
(fact r)))
(def ncr choose)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment