Skip to content

Instantly share code, notes, and snippets.

@darkf

darkf/main.fs

Created Mar 17, 2012
Embed
What would you like to do?
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
You can’t perform that action at this time.