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 / j.c
Created April 6, 2017 01:01
Arthur Whitney's mini J interpreter in C from "Remembering Ken Iverson" http://keiapl.org/rhui/remember.htm
typedef char C;typedef long I;
typedef struct a{I t,r,d[3],p[2];}*A;
#define P printf
#define R return
#define V1(f) A f(w)A w;
#define V2(f) A f(a,w)A a,w;
#define DO(n,x) {I i=0,_n=(n);for(;i<_n;++i){x;}}
I *ma(n){R(I*)malloc(n*4);}mv(d,s,n)I *d,*s;{DO(n,d[i]=s[i]);}
tr(r,d)I *d;{I z=1;DO(r,z=z*d[i]);R z;}
A ga(t,r,d)I *d;{A z=(A)ma(5+tr(r,d));z->t=t,z->r=r,mv(z->d,d,r);
@jdh30
jdh30 / bf.bf
Created April 6, 2017 01:03
BF interpreter written in BF from "A very short self-interpreter" https://arxiv.org/html/cs/0311032
>>>+[[-]>>[-]++>+>+++++++[<++++>>++<-]++>>+>+>+++++[>
++>++++++<<-]+>>>,<++[[>[->>]<[>>]<<-]<[<]<+>>[>]>[<+
>-[[<+>-]>]<[[[-]<]++<-[<+++++++++>[<->-]>>]>>]]<<]<]
<
[[<]>[[>]>>[>>]+[<<]<[<]<+>>-]>[>]+[->>]<<<<[[<<]<[<]
+<<[+>+<<-[>-->+<<-[>+<[>>+<<-]]]>[<+>-]<]++>>-->[>]>
>[>>]]<<[>>+<[[<]<]>[[<<]<[<]+[-<+>>-[<<+>++>-[<->[<<
+>>-]]]<[>+<-]>]>[>]>]>[>>]>>]<<[>>+>>+>>]<<[->>>>>>>
>]<<[>.>>>>>>>]<<[>->>>>>]<<[>,>>>]<<[>+>]<<[+<<]<]
@jdh30
jdh30 / FemtoMLParserUsingFParsec.fs
Last active June 6, 2020 03:48
FemtoML parser in F# using FParsec
> let pInt : Parser<_, unit> = puint32 |>> int .>> spaces;;
> let str s = pstring s >>. spaces;;
> let keywords =
"if then else let rec in fun".Split ' '
|> set;;
> let pIdent : Parser<_, unit> =
@jdh30
jdh30 / FemtoMLParserUsingFSLexAndYacc.fs
Created April 15, 2017 22:06
FemtoML parser in F# using fslex and fsyacc
{
open Parse
open Lexing
let ident = function
| "let" -> LET
| "rec" -> REC
| "in" -> IN
| "fun" -> FUN
| "if" -> IF
| "then" -> THEN
@jdh30
jdh30 / FemtoMLParserUsingActivePatterns.fs
Last active May 22, 2019 05:12
FemtoML parser in F# using active patterns
> let alpha = set['a'..'z'] + set['A'..'Z'];;
> let num = set['0'..'9'];;
> let alphanum = alpha + num;;
> let (|Char|_|) alphabet = function
| c::cs when Set.contains c alphabet -> Some(c, cs)
| _ -> None;;
@jdh30
jdh30 / ConcestorMap.fs
Created October 9, 2017 19:54
Persistent Map implemented as a Concestor data structure
// This code implements a persistent dictionary similar to Map except the
// internal representation is a Dictionary + diffs. This offers different
// performance tradeoffs and, in particular, much faster add and remove when
// used linearly.
//
// For more details, see the following articles from the F# Journal:
//
// http://fsharpnews.blogspot.co.uk/2017/10/the-concestor-set.html
// http://fsharpnews.blogspot.co.uk/2017/10/a-simple-concestor-dictionary.html
@jdh30
jdh30 / InstallOCaml.sh
Last active December 13, 2020 08:04
Script to install and initialize OCaml for microservice development (TCP+HTTP+JSON) on an Ubuntu Linux box (e.g. in Amazon's AWS Cloud)
# .NET Core 2.1 with Ubuntu Server 18.04 - Version 1.0 - ami-f4f4cf91
sh <(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)
sudo apt-get update
sudo apt-get install make m4 gcc zip aspcud emacs libssl-dev ncurses-dev pkg-config tuareg-mode libffi-dev bubblewrap linux-tools-common linux-tools-aws
opam init
eval $(opam env)
opam install ocamlfind core async async_ssl cohttp yojson ppx_deriving ocp-indent merlin cohttp-async
@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 / 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