Created
August 5, 2010 16:44
-
-
Save yogthos/509994 to your computer and use it in GitHub Desktop.
infix math notation example
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 {* 1, / 1, + 2, - 2}) | |
(defn formula [& exprs] | |
(let [[x aop y bop z] (take 5 exprs)] | |
(if aop | |
(if bop | |
(if (< (get precedence aop) (get precedence bop)) | |
(recur (into [(aop (formula x) (formula y))] (drop 3 exprs))) | |
(recur (into [x aop (bop (formula y) (formula z))] (drop 5 exprs)))) | |
(aop (formula x) (formula y))) | |
(if (vector? x) | |
(apply formula x) x) | |
))) | |
(formula 5 + 3 * [[2 + 5] - [[2 + 8 * 10 / 11 + 12 - 3 - 9] / 6]]) | |
;output: 235/11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment