Skip to content

Instantly share code, notes, and snippets.

@yogthos
Created August 5, 2010 16:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogthos/509994 to your computer and use it in GitHub Desktop.
Save yogthos/509994 to your computer and use it in GitHub Desktop.
infix math notation example
(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