Skip to content

Instantly share code, notes, and snippets.

@takemyoxygen
Created July 2, 2019 20:53
Show Gist options
  • Save takemyoxygen/1fd9711cefd2d68aa7b69404c0be4356 to your computer and use it in GitHub Desktop.
Save takemyoxygen/1fd9711cefd2d68aa7b69404c0be4356 to your computer and use it in GitHub Desktop.
(defn in?
[xs x]
(some #(= x %) xs))
(defn unwrap
[xs]
(if (and (list? xs) (list? (first xs)) (nil? (next xs)))
(first xs)
xs))
(defn replace-op
[operators expr]
(let [[left op right & rest] expr]
(println (str "left: " left ", op: " op ", right: " right ", rest: " rest))
(cond
(nil? op) (list left)
(in? operators op) (replace-op operators (cons (list op left right) rest))
:else (concat (list left op) (replace-op operators (cons right rest))))))
(defmacro eval-arithmetic
[expression]
(->> expression
(replace-op ['* '/])
(replace-op ['+ '-])
unwrap))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment