Skip to content

Instantly share code, notes, and snippets.

@ibigbug
Last active December 14, 2015 10:10
Show Gist options
  • Save ibigbug/5070264 to your computer and use it in GitHub Desktop.
Save ibigbug/5070264 to your computer and use it in GitHub Desktop.
Solve the derived function of a given function.
(defn diff [f]
(fn [x]
(let [dx (/ 1 Integer/MAX_VALUE)] ;nearly 0
(/ (- (apply f [(+ x dx)]) (apply f [x])) dx))))
(defn g [x] ; given function
(* x x))
(def dg (diff g)) ; get dg
(print (float (dg 3))) ; 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment