This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 + x_ := x | |
x_ + 0 := x | |
f_ + (g_ + h_) := f + g + h | |
0 x_ := 0 | |
x_ 0 := 0 | |
1 x_ := x | |
x_ 1 := x | |
f_ (g_ h_) := f g h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
D[_, _] := 0 | |
D[x_, x_] := 1 | |
D[f_ + g_, x_] := D[f, x] + D[g, x] | |
D[f_ - g_, x_] := D[f, x] - D[g, x] | |
D[f_ g_, x_] := f D[g, x] + g D[f, x] | |
D[f_ / g_, x_] := f D[1/g, x] + 1/g D[f, x] | |
D[f_^g_, x_] := f^(g - 1) (g D[f, x] + f Log[f] D[g, x]) | |
D[Log[f_], x_] := D[f, x] / f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 >> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
D(x, y) -> 0 | |
D(x, x) -> 1 | |
D(f+g, x) -> D(f, x) + D(g, x) | |
D(f*g, x) -> f*D(g, x) + g*D(f, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let (|Float|_|) s = | |
let mutable x = 0.0 | |
if System.Double.TryParse(s, &x) then Some x else None | |
let binop f x y stack = | |
let z = f x y | |
printfn "%A" z | |
z::stack | |
let rec loop stack = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let (|Float|_|) s = | |
let mutable x = 0.0 | |
if System.Double.TryParse(s, &x) then Some x else None | |
let rec (|Parse|) = function | |
| Float x::t -> <@ x @>, t | |
| "+"::Parse(x, Parse(y, t)) -> <@ %x + %y @>, t | |
| "*"::Parse(x, Parse(y, t)) -> <@ %x * %y @>, t | |
let (Parse(f, _)) = stdin.ReadToEnd().Split '\n' |> List.ofSeq |> List.rev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let (|Int|_|) s = | |
let mutable x = 0 | |
if System.Int32.TryParse(s, &x) then Some x else None | |
LLVM.NativeLibrary.LLVMDLL.Load() | |
LLVM.Target.InitializeNative() | |
let context = LLVM.Context.Global | |
let i32 = LLVM.IntegerType.GetInt32 context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Collections.Generic | |
/// Imperative depth-first search | |
let dfs (V, E) = | |
let visited = HashSet(HashIdentity.Structural) | |
let stack = Stack[V] | |
while stack.Count > 0 do | |
let u = stack.Pop() | |
if not(visited.Contains u) then | |
for v in E u do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>>+[[-]>>[-]++>+>+++++++[<++++>>++<-]++>>+>+>+++++[> | |
++>++++++<<-]+>>>,<++[[>[->>]<[>>]<<-]<[<]<+>>[>]>[<+ | |
>-[[<+>-]>]<[[[-]<]++<-[<+++++++++>[<->-]>>]>>]]<<]<] | |
< | |
[[<]>[[>]>>[>>]+[<<]<[<]<+>>-]>[>]+[->>]<<<<[[<<]<[<] | |
+<<[+>+<<-[>-->+<<-[>+<[>>+<<-]]]>[<+>-]<]++>>-->[>]> | |
>[>>]]<<[>>+<[[<]<]>[[<<]<[<]+[-<+>>-[<<+>++>-[<->[<< | |
+>>-]]]<[>+<-]>]>[>]>]>[>>]>>]<<[>>+>>+>>]<<[->>>>>>> | |
>]<<[>.>>>>>>>]<<[>->>>>>]<<[>,>>>]<<[>+>]<<[+<<]<] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let timer = System.Diagnostics.Stopwatch.StartNew() | |
type expr = | |
| Int of int | |
| Var of string | |
| Add of expr * expr | |
| Mul of expr * expr | |
| Pow of expr * expr | |
| Ln of expr |