Created
August 7, 2010 13:19
-
-
Save yogthos/512808 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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