Skip to content

Instantly share code, notes, and snippets.

@mattias-lw
Created May 10, 2012 22:17
Show Gist options
  • Save mattias-lw/2656254 to your computer and use it in GitHub Desktop.
Save mattias-lw/2656254 to your computer and use it in GitHub Desktop.
RPN i Clojure
(defn rpn [expr]
(let [head (first expr)
tail (rest expr)]
(cond
(nil? head) (fn [n] n)
(number? head) ((rpn tail) head)
:else (let [restex (rpn tail)]
(fn [n]
(fn [m]
(restex ((resolve (symbol head)) m n))
)
)
)
)
)
)
(defn lex [input]
(map (fn [t]
(cond
(re-matches #"\d+" t) (Integer/parseInt t)
:else (symbol t)
)
) (re-seq #"\S+" input)
)
)
(while true (println (rpn (lex (read-line)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment