Created
May 10, 2012 22:17
-
-
Save mattias-lw/2656254 to your computer and use it in GitHub Desktop.
RPN i Clojure
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
(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