Skip to content

Instantly share code, notes, and snippets.

@jasonjckn
Created May 17, 2011 19:39
Show Gist options
  • Select an option

  • Save jasonjckn/977216 to your computer and use it in GitHub Desktop.

Select an option

Save jasonjckn/977216 to your computer and use it in GitHub Desktop.
(ns calc (:gen-class))
(defmulti calc (fn [t] (if (seq? t) (first t) :num)))
(defn def-prefix-op [sym f]
(defmethod calc sym [[op & args]] (apply f (map calc args))))
(doseq [[sym op] [['+ +] ['- -]
['* *] ['/ /]
['sin #(Math/sin %)]]]
(def-prefix-op sym op))
(defmethod calc :num [n] n)
(defn -main []
(letfn [(prompt [] (print "=>") (flush) (read-line))]
(println (calc (read-string (prompt)))))
(recur))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment