Skip to content

Instantly share code, notes, and snippets.

Avatar

Jon Harrop jdh30

View GitHub Profile
@jdh30
jdh30 / Eddy.fs
Created Feb 28, 2019
Memory management benchmark - shedding Eddys
View Eddy.fs
(*
This benchmark maintains an array of roots and randomly either pushes
onto them and relinks them or empties them. If I designed it correctly
then this should rapidly create unreachable subgraphs, some of which
are cyclic.
This is a torture test for any kind of reference counting.
*)
type Vertex = { mutable Dst: Vertex }
@jdh30
jdh30 / promptness.fs
Last active Feb 28, 2019
A program for which prompt collection is practically impossible
View promptness.fs
(*
The resizable array xs is required for the first half of the execution of
this "test" function but not the last half. Collecting xs as early as possible
would mean collecting it halfway through the execution of "test" but this
is practically impossible. Doing so in general is equivalent to solving the
halting problem.
*)
let next n =
6364136223846793005UL*n + 1442695040888963407UL
@jdh30
jdh30 / JsonParser.fs
Last active Jun 6, 2020
Simple JSON parser written in F# using active patterns
View JsonParser.fs
type Json =
| Null
| Bool of bool
| Number of float
| String of string
| Array of Json list
| Object of (string * Json) list
type Bracket = Open | Close
@jdh30
jdh30 / ray.jl
Last active Sep 17, 2019
Ray tracer in Julia
View ray.jl
// Translated from the Ray Tracer Language Comparison
// http://www.ffconsultancy.com/languages/ray_tracer/benchmark.html
import Printf
struct Hit
l ::Float64
d ::Array{Float64, 1}
end
@jdh30
jdh30 / Q_rsqrt.fs
Last active Oct 13, 2018
Fast inverse sqrt in F#
View Q_rsqrt.fs
// The famous fast inverse square root approximation from
// Quake ported to F#.
// Note: this algorithm is not fast on modern computers
// and this implementation of it is extremely inefficient:
// for educational purposes only!
open System
let isqrt y =
let x2 = y * 0.5f
@jdh30
jdh30 / Camlp4LikeParser.fs
Created Mar 12, 2018
A parser definition in F# written in the style of a Camlp4 parser
View Camlp4LikeParser.fs
let reint = R(Regex "[0-9]+")
let relident = R(Regex "[a-z][a-zA-Z0-9]*")
let grammar : Grammar<Entry> =
[
Expr,
[ [ S"if"; C Expr; S"then"; C Expr; S"else"; C Expr ], fun [E p; E t; E f] -> E(If(p, t, f))
[ C Expr; S"<="; C Expr ], fun [E e1; E e2] -> E(BinOp(e1, Le, e2))
[ C Expr; S"+"; C Expr ], fun [E e1; E e2] -> E(BinOp(e1, Add, e2))
[ C Expr; S"-"; C Expr ], fun [E e1; E e2] -> E(BinOp(e1, Sub, e2))
@jdh30
jdh30 / SimpleParser1.fs
Created Mar 4, 2018
Port of an camlp4-based inline parser to vanilla ML
View SimpleParser1.fs
// See: https://gist.github.com/jdh30/6130c615b5945fd57fc0ea74fcb87e05
open System.Text.RegularExpressions
type BinOp = Add | Sub | Le
type expr =
| Int of int
| Var of string
| BinOp of expr * BinOp * expr
@jdh30
jdh30 / MiniML.ml
Created Mar 4, 2018
LLVM-based compiler written in ~100 lines of OCaml
View MiniML.ml
(* See https://groups.google.com/forum/#!msg/fa.caml/i6IgSFX8XkY/4khF8z1V7loJ *)
type expr =
| Int of int
| Var of string
| BinOp of [ `Add | `Sub | `Leq ] * expr * expr
| If of expr * expr * expr
| Apply of expr * expr
type defn = LetRec of string * string * expr
@jdh30
jdh30 / deriv.fs
Created Dec 26, 2017
F# code to compute the nth derivative of x^x
View deriv.fs
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
@jdh30
jdh30 / deriv.mx
Created Dec 26, 2017
Mathematica code to compute the nth derivative of x^x (using specific rewrite rules)
View deriv.mx
pown[_,0]:=1
pown[a_,1]:=a
pown[a_,n_]:=With[{b=pown[a,Floor[n/2]]},b b If[Mod[n,2]==0,1,a]]
add[m_Integer,n_Integer]:=m+n
add[0,f_]:=f
add[f_,0]:=f
add[f_,n_Integer]:=add[n,f]
add[f_,add[n_Integer,g_]]:=add[n,add[f,g]]
add[add[f_,g_],h_]:=add[f,add[g,h]]
You can’t perform that action at this time.