Created
June 7, 2017 08:35
-
-
Save erdos/8a081c0ebbf5e6f0d3c04f6714c574d6 to your computer and use it in GitHub Desktop.
Simple Reverse Polish Notation Calculator in 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 [s] | |
(first (reduce (fn [[s1 s2 & ss :as stack] op] | |
(case op | |
+ (cons (+ s1 s2) ss) | |
- (cons (- s1 s2) ss) | |
* (cons (* s1 s2) ss) | |
/ (cons (/ s1 s2) ss) | |
, (cons op stack))) | |
() (read-string (str \( s \)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this should also do it.