Skip to content

Instantly share code, notes, and snippets.

@teepark
Last active August 29, 2015 14:18
Show Gist options
  • Save teepark/96887c70aa32ebf61355 to your computer and use it in GitHub Desktop.
Save teepark/96887c70aa32ebf61355 to your computer and use it in GitHub Desktop.
; problem 5 from https://aphyr.com/posts/305-clojure-from-the-ground-up-macros
(defmacro exact [expr]
`(let [~'+ (fn ([& a#] (apply + (map rationalize a#))))
~'- (fn ([& a#] (apply - (map rationalize a#))))
~'* (fn ([& a#] (apply * (map rationalize a#))))
~'/ (fn ([& a#] (apply / (map rationalize a#))))]
~expr))
(let [* (fn ([& a] (apply * (map rationalize a))))]
(* 2452.45 100))
(exact (* 2452.45 100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment