Skip to content

Instantly share code, notes, and snippets.

@jdh30
jdh30 / cc500.c
Created July 24, 2025 14:43
A self-compiling compiler for a small subset of C (by Edmund Grimley-Evans, 2006)
/*
* Copyright (C) 2006 Edmund GRIMLEY EVANS <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
@jdh30
jdh30 / prompts.md
Last active July 21, 2025 08:45
LLM prompts

LLM prompts

Write an interpreter in OCaml

Write an interpreter in OCaml 5 that fits in a single file that can parse and interpret the string "let rec fib n =\n  if n<2 then n else\n    fib(n-2)+fib(n-1) in\nfib 30".

Generate synthetic data ready for fine tuning

Generate 10 prompt-completion pairs where the prompt contains a non-trivial integer arithmetic calculation (using +, -, *, / and brackets) and the completion provides a thought process using a REPL before stating the answer.
@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 / 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 / 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 / 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
@jdh30
jdh30 / KMP.tq
Last active November 21, 2022 17:06
Knuth-Morris-Pratt string search algorithm
let search p =
let m = # p in
let next = Array.init m [_ -> 0] in
let rec init(i, j) =
if i >= m-1 then () else
if Array.get p i = Array.get p j then
let () = Array.Unsafe.set next (i+1) (j+1) in
init(i+1, j+1)
else
if j=0 then init(i+1, j) else init(i, Array.get next j) in
@jdh30
jdh30 / Base64.tq
Created November 21, 2022 16:39
Base64 encoder
type State =
| S0
| S1 Number
| S2 Number
let charOfNumber n =
if n<26 then Char.ofNumber(n+65)
else if n<52 then Char.ofNumber(n-26+97)
else if n<62 then Char.ofNumber(n-52+48)
else if n=62 then '+' else '/'
@jdh30
jdh30 / SuffixArray.tq
Created November 21, 2022 16:23
Compute the suffix array of an array
let suffixArray str =
let g = Array.get in
let n = # str in
let sa = Array.init n id in
let pos = Array.copy str in
let tmp = Array.init n [_ -> 0] in
let sufCmp gap (i, j) =
compare (g pos i, g pos j) @
[ Equal ->
if i+gap >= n || j+gap >= n then compare(j, i) else
@jdh30
jdh30 / MaxSumSubseq.tq
Created November 21, 2022 16:09
Maximum sum subsequence
let maxSumSubseq a =
let maxm = Array.fold [a, b -> max a b] 0 a in
let rec loop sum i =
if i = # a then sum else
let ai = Array.get a i in
if ai > 0 then loop (sum + ai) (i+1) else loop sum (i+1) in
loop 0 0