Skip to content

Instantly share code, notes, and snippets.

Avatar

Jon Harrop jdh30

View GitHub Profile
@jdh30
jdh30 / deriv.ml
Created Dec 24, 2017
OCaml code to compute the nth derivative of x^x
View deriv.ml
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 Mar 8, 2019
Swift code to compute the nth derivative of x^x
View deriv.swift
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 / InstallOCaml.sh
Last active Dec 13, 2020
Script to install and initialize OCaml for microservice development (TCP+HTTP+JSON) on an Ubuntu Linux box (e.g. in Amazon's AWS Cloud)
View InstallOCaml.sh
# .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 / ConcestorMap.fs
Created Oct 9, 2017
Persistent Map implemented as a Concestor data structure
View ConcestorMap.fs
// 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 / FemtoMLParserUsingActivePatterns.fs
Last active May 22, 2019
FemtoML parser in F# using active patterns
View FemtoMLParserUsingActivePatterns.fs
> 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 / FemtoMLParserUsingFSLexAndYacc.fs
Created Apr 15, 2017
FemtoML parser in F# using fslex and fsyacc
View FemtoMLParserUsingFSLexAndYacc.fs
{
open Parse
open Lexing
let ident = function
| "let" -> LET
| "rec" -> REC
| "in" -> IN
| "fun" -> FUN
| "if" -> IF
| "then" -> THEN
@jdh30
jdh30 / FemtoMLParserUsingFParsec.fs
Last active Jun 6, 2020
FemtoML parser in F# using FParsec
View FemtoMLParserUsingFParsec.fs
> 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 / bf.bf
Created Apr 6, 2017
BF interpreter written in BF from "A very short self-interpreter" https://arxiv.org/html/cs/0311032
View bf.bf
>>>+[[-]>>[-]++>+>+++++++[<++++>>++<-]++>>+>+>+++++[>
++>++++++<<-]+>>>,<++[[>[->>]<[>>]<<-]<[<]<+>>[>]>[<+
>-[[<+>-]>]<[[[-]<]++<-[<+++++++++>[<->-]>>]>>]]<<]<]
<
[[<]>[[>]>>[>>]+[<<]<[<]<+>>-]>[>]+[->>]<<<<[[<<]<[<]
+<<[+>+<<-[>-->+<<-[>+<[>>+<<-]]]>[<+>-]<]++>>-->[>]>
>[>>]]<<[>>+<[[<]<]>[[<<]<[<]+[-<+>>-[<<+>++>-[<->[<<
+>>-]]]<[>+<-]>]>[>]>]>[>>]>>]<<[>>+>>+>>]<<[->>>>>>>
>]<<[>.>>>>>>>]<<[>->>>>>]<<[>,>>>]<<[>+>]<<[+<<]<]
@jdh30
jdh30 / j.c
Created Apr 6, 2017
Arthur Whitney's mini J interpreter in C from "Remembering Ken Iverson" http://keiapl.org/rhui/remember.htm
View j.c
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 / dfs.fs
Created Apr 4, 2017
Imperative and purely functional depth-first searches in F#
View dfs.fs
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
You can’t perform that action at this time.