Skip to content

Instantly share code, notes, and snippets.

@yogthos
Created August 7, 2010 13:19
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 yogthos/512808 to your computer and use it in GitHub Desktop.
Save yogthos/512808 to your computer and use it in GitHub Desktop.
(def precedence {mod 0, * 1, / 1, + 2, - 2})
(defn find-split [exprs]
(loop [op-pos 1
items exprs]
(let [[x opa y opb] (take 4 items)]
(if (and opa opb (> (get precedence opa) (get precedence opb)))
(recur
(+ op-pos 2)
(drop 2 items))
op-pos)) ))
(defn formula [& exprs]
(if (= (count exprs) 1)
(if (number? (first exprs))
(first exprs)
(apply formula (first exprs)))
(let [split (split-at (dec (find-split exprs)) exprs)
[x op y] (take 3 (second split))]
(recur (into (conj
(vec (first split))
(op (formula x) (formula y)))
(vec (drop 3 (second split))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment