Skip to content

Instantly share code, notes, and snippets.

@darkf
Created March 17, 2012 06:40
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 darkf/2055832 to your computer and use it in GitHub Desktop.
Save darkf/2055832 to your computer and use it in GitHub Desktop.
calculus assignment bs
(* Take a tree of expressions in and output the expression tree of the differentiation of it. (or something like that.)
Copyright (c) 2012 darkf
MIT license
*)
(* Assignment: http://pastebin.com/tiRVsxNx
Graphs: http://f.imgtmp.com/FOVY7.jpg
*)
type Node =
| Constant of int
| Variable of string
| Mult of Node * Node
| Plus of Node * Node
| Minus of Node * Node
| Cos of Node
| Sin of Node
(*let tree = Mult ((Variable "x"), Plus (
(Plus ((Constant 2), (Variable "x")))
,
(Cos
(Minus ((Variable "x"), (Constant 4))))))*)
let tree = Mult (Constant 5, Variable "x")
let rec diff x = match x with
| Plus(x,y) -> Plus (diff x, diff y)
| Minus(x,y) -> Minus (diff x, diff y)
| Mult(x,y) -> Plus(
Mult (diff x, y),
Mult (x, diff y)
)
//| Cos(x) ->
//| Sin(x) ->
| Constant(x) -> Constant 0
| Variable(x) -> Constant 1
printfn "%A" (diff tree)
System.Console.ReadKey(true) |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment