Skip to content

Instantly share code, notes, and snippets.

@jdh30
jdh30 / aa.tq
Created November 20, 2022 23:05
Balanced binary (AA) search trees
type rec AATree a = E | T(Number, AATree a, a, AATree a)
let empty = E
let isEmpty = [E → True | T _ → False]
let rec count =
[ E → 0
| T(_, l, _, r) → count l + 1 + count r ]
@jdh30
jdh30 / ray.tq
Last active November 21, 2022 01:14
Ray tracer with hierarchical spherical bounding regions
let zero = 0, 0, 0
let scale s (x, y, z) = s*x, s*y, s*z
let add (x1,y1,z1) (x2,y2,z2) = x1+x2, y1+y1, z1+z2
let sub (x1,y1,z1) (x2,y2,z2) = x1-x2, y1-y1, z1-z2
let dot (x1,y1,z1) (x2,y2,z2) = x1*x2 + y1*y1 + z1*z2
let unitise u = scale (1/√(dot u u)) u
type rec Scene =
| Sphere
| Bound(Array((Number, Number, Number), Number, Scene))
@jdh30
jdh30 / concestor.ml
Last active February 21, 2022 00:40
Concestor in OCaml: a purely functional Map backed by a hash table
module Concestor(Key: Collections.HASH) : sig
type 'v t
val empty : unit -> 'v t
val is_empty : 'v t -> bool
val add : Key.t -> 'v -> 'v t -> 'v t
val remove : Key.t -> 'v t -> 'v t
val mem : Key.t -> 'v t -> bool
val find : Key.t -> 'v t -> 'v
val find_opt : Key.t -> 'v t -> 'v option
@jdh30
jdh30 / hashTable.ml
Created February 21, 2022 00:37
Open addressed hash table in OCaml
let primes =
[|3; 7; 13; 31; 61; 127; 251; 509; 1021; 2039; 4093; 8191; 16381; 32749;
65521; 131071; 262139; 524287; 1048573; 2097143; 4194301; 8388593;
16777213; 33554393; 67108859; 134217689; 268435399; 536870909; 1073741789;
2147483647|]
let mods =
[|(fun n -> n mod 3);
(fun n -> n mod 7);
(fun n -> n mod 13);
@jdh30
jdh30 / upload.sh
Last active May 17, 2021 21:51
Upload files from this machine to another without clobbering.
# Upload files from this machine to another without clobbering.
# Usage:
# ./upload.sh <srcdir> <user@host> <dstdir>
tar -cz $1 | ssh $2 "tar -kzxf - -C ${3}"
@jdh30
jdh30 / MoreArrayParallel.fs
Created March 15, 2021 11:12
More functions for F#'s Array.Parallel module
open System.Threading
/// Spawn a new Task.
let task f x =
Tasks.Task<_>.Factory.StartNew(fun () -> f x)
module internal Internal =
/// Compute "map f [i0, i2) |> reduce g" in parallel using divide and conquer.
/// Assumes "f" is associative but does not assume that it is commutative.
/// Therefore, this function can return "f (f 0 1) (f 2 3)" but not "f 1 0" etc.
@jdh30
jdh30 / SpellCorrektor.fs
Created March 14, 2021 18:24
Peter Norvig's spelling corrector written in F#
open System.Text.RegularExpressions
let alphabet = ['a'..'z']
let edits1 (w: string) =
seq { for i in 0 .. w.Length do
if i < w.Length then
yield w.[0..i-1] + w.[i+1..] // Delete
if i < w.Length-1 then
yield w.[0..i-1] + w.[i+1..i+1] + w.[i..i] + w.[i+2..] // Swap
@jdh30
jdh30 / compiler7.fs
Created March 14, 2021 17:42
F# version of my tiny ARM A32 compiler version 7
(*
This tiny 211-line compiler converts programs written in a little ML dialect into 32-bit ARM
assembly.
This program is most easily run using FSI with flags to turn off FSI's output so the output
of this program can be piped into a file:
dotnet fsi --quiet --exec compiler7.fs >fib.s
That file can then be compiled with:
@jdh30
jdh30 / Init.fs
Last active February 28, 2020 01:05
Avoiding the undebuggable type initialization exception in F#
let mutable globalException : System.Exception = null // Compiler generated
let myGlobal =
if isNull globalException then // Compiler generated
try // Compiler generated
System.IO.File.ReadAllText "DoesNotExist.txt"
with e -> // Compiler generated
globalException <- e // Compiler generated
Unchecked.defaultof<_> // Compiler generated
else Unchecked.defaultof<_> // Compiler generated
@jdh30
jdh30 / DraggableRectangles.fs
Created September 17, 2019 01:23
F#+WPF solution to the Draggable Rectangles challenge by Panicz Godek
open System.Windows
let goldenRatio = (1.0 + sqrt 5.0) / 2.0
let newBrush =
let mutable hue = 0.0
fun () ->
hue <- hue + System.Math.PI / goldenRatio
let s x = byte(255.0 * x)
let c x = 0.5 * (cos x + 1.0)