Skip to content

Instantly share code, notes, and snippets.

@jdh30
jdh30 / dfs.fs
Created April 4, 2017 16:56
Imperative and purely functional depth-first searches in F#
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
@jdh30
jdh30 / gist:68d2b1e4d59123a8da944d578a7080ba
Last active October 29, 2024 17:41
Fix OCaml VSCode extension on Mac

Fix OCaml VSCode extension on Mac

On Mac, the OCaml extension for VSCode scribbles inferred information everywhere which makes development much harder. This line noise is called "inlay hints". They can be disabled by editing the file:

~/Library/Application\ Support/Code/User/settings.json

To include the line:

"editor.inlayHints.enabled": "off",
@jdh30
jdh30 / InstallOCaml.sh
Last active September 28, 2024 10:30
Script to install and initialize OCaml for microservice development (TCP+HTTP+JSON) on an Ubuntu Linux box (e.g. in Amazon's AWS Cloud)
sudo apt-get update
sudo apt-get install opam elpa-tuareg make m4 gcc zip aspcud emacs libssl-dev ncurses-dev pkg-config libffi-dev bubblewrap linux-tools-common linux-tools-aws
opam init
eval $(opam env --switch=default)
opam install ocamlfind core async async_ssl cohttp yojson ppx_deriving ocp-indent merlin cohttp-async ocamlformat utop
@jdh30
jdh30 / InstallOctoprint
Created December 26, 2023 03:11
Install Octoprint on a Raspberry Pi 5
python -m venv OctoPrint
OctoPrint/bin/pip install OctoPrint
wget https://github.com/OctoPrint/OctoPrint/raw/master/scripts/octoprint.service
emacs octoprint.service
sudo mv octoprint.service /etc/systemd/system/octoprint.service
sudo systemctl enable octoprint.service
sudo service octoprint start
@jdh30
jdh30 / gist:92a382c6a5c1b20cc9541e0ab776860b
Created March 27, 2024 17:02
Install OpenHantek6022 on Raspberry Pi 5
sudo apt install cmake libfftw3-dev qtbase5-dev qttools5-dev libusb-1.0-0-dev
git clone https://github.com/OpenHantek/OpenHantek6022
cd OpenHantek6022
mkdir build
cd build
cmake ..
make
sudo cp ../utils/udev_rules/60-openhantek.rules /etc/udev/rules.d/
@jdh30
jdh30 / deriv.ml
Created December 24, 2017 04:13
OCaml code to compute the nth derivative of x^x
open Printf
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.swift
Last active March 11, 2024 14:49
Swift code to compute the nth derivative of x^x
enum Expr {
case Int(n: Int)
case Var(x: String)
indirect case Add(f: Expr, g: Expr)
indirect case Mul(f: Expr, g: Expr)
indirect case Pow(f: Expr, g: Expr)
indirect case Ln(f: Expr)
}
func pown(a: Int, b: Int) -> Int {
@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 / JsonParser.fs
Last active January 30, 2024 14:06
Simple JSON parser written in F# using active patterns
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 / PredatorPrey.tq
Created November 21, 2022 22:30
Simulating predator-prey dynamics
let g = 0.02 and k = 500 and t = 1500
let evolve(r, f) =
let dtrf = 0.0001 * r * f in
Some((r, f), (r + (1.0 - r/k)*r*g - dtrf, dtrf + (1.0 - g)*f))
let () =
Seq.unfold evolve (50, 10)
@ Seq.truncate 1500
@ Array.ofSeq