Skip to content

Instantly share code, notes, and snippets.

type AccountKind = Simple | Valuable | MostValuable
type CustomerStatus =
| Unregistered
| Registered of AccountKind * Years:int
let accountFactor kind =
match kind with
| Simple -> 0.9m
| Valuable -> 0.7m
let accountFactor = function
| `Simple -> 0.9
| `Valuable -> 0.7
| `MostValuable -> 0.5
let loyaltyFactor years =
1.0 -. float(min years 5) /. 100.0
let applyDiscount price = function
| `Unregistered -> price
@jdh30
jdh30 / Derivative
Last active February 25, 2017 10:49
Rewrite rules for simple differentiation in Mathematica syntax
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
@jdh30
jdh30 / Simplify.mx
Last active February 16, 2017 05:55
Symbolic simplification rules written in Mathematica's language
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
@jdh30
jdh30 / MicroKanren.fs
Created February 18, 2017 15:26
MicroKanren ported from Scheme to F#
type Term =
| Var of int
| Pair of Term * Term
| Int of int
let (|Find|_|) s k = Map.tryFind k s
let rec walk (s: Map<_,_>) = function
| Var(Find s t) -> walk s t
| t -> t
@jdh30
jdh30 / derivative.ml
Last active February 25, 2017 22:09
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 >>
@jdh30
jdh30 / derivative.rw
Created February 25, 2017 22:35
A symbolic derivative written in my own term rewrite language
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)
@jdh30
jdh30 / ReversePolishCalculator.fs
Created March 25, 2017 13:58
Reverse polish calculator in F#, derived from this 35-line Racket metaprogramming example http://beautifulracket.com/stacker/source-listing.html
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 =
@jdh30
jdh30 / ReversePolishCompiler1.fs
Created March 26, 2017 00:18
Reverse polish calculator compiler in F# using quotations, derived from this 35-line Racket example http://beautifulracket.com/stacker/source-listing.html Raw
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
@jdh30
jdh30 / ReversePolishCompiler2.fs
Created March 26, 2017 00:34
Reverse polish compiler in F# using LLVM, derived from this 35-line Racket example http://beautifulracket.com/stacker/source-listing.html
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