Skip to content

Instantly share code, notes, and snippets.

@jdh30
Last active February 25, 2017 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdh30/91af8fe2120d957a7ba8b0faf8a54072 to your computer and use it in GitHub Desktop.
Save jdh30/91af8fe2120d957a7ba8b0faf8a54072 to your computer and use it in GitHub Desktop.
A symbolic derivative written in OCaml using pattern matching over code quotations
let d x = function
| <:expr< $y$ >> when x=y -> <:expr< 1 >>
| <:expr< $f$ + $g$ >> -> <:expr< $d f x$ + $d g x$ >>
| <:expr< $f$ * $g$ >> -> <:expr< $f$ * $d g x$ + $g$ * $d f x$ >>
| _ -> <:expr< 0 >>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment