Skip to content

Instantly share code, notes, and snippets.

View lindig's full-sized avatar

Christian Lindig lindig

View GitHub Profile
@lindig
lindig / collatz.ml
Last active December 29, 2015 20:19
Diskussion on Hacker News about finding the longest Collatz (-like) sequence. The discussion compared a Haskell implementation with an implementation in C. This adds just another implementation.
(* Discussion on Hacker News: https://news.ycombinator.com/item?id=6822901
*)
exception Error of string
let (@@) f x = f x
let error fmt = Printf.kprintf (fun msg -> raise (Error msg)) fmt
type result = int * int
@lindig
lindig / hello.ml
Created September 7, 2015 09:43
hello.ml - discussion about using open in OCaml
let rec join = function
| [] -> ""
| [x] -> x
| [x;y] -> x^" and "^y
| x::xs -> x ^ ", " ^ join xs
let main () =
let argv = Array.to_list Sys.argv in
@lindig
lindig / tokenizer.mll
Created September 9, 2015 16:45
Some Scanning Recipes for OCamlLex
{
(* short names for important modules *)
module L = Lexing
module B = Buffer
type token =
| STR of string
| INT of int
| ID of string
| PLUSEQ
@lindig
lindig / LaTeX.svg
Last active October 8, 2015 07:55
LaTeX Logo With Characters Joined
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lindig
lindig / ring3fmt.ml
Last active September 18, 2016 13:48
A text filter that expands leading whitespace to tabs and but other tabs to spaces.
(*
* filter that expands leading whitespace to tabs and but other tabs
* to spaces.
*
* ocamlbuild ring3fmt.native
*)
(** create a string with [n] [chr] chars *)
let ( ** ) chr n = String.make n chr
@lindig
lindig / chopchop.mll
Created June 13, 2016 08:13
Slide a window over a text read from stdin and emit all windows contents.
{ (* vim: set ts=2 sw=2 et: *)
(* This tool:
* - reads input from stdin line by line
* - seperates each line into a list of words
* - slides a window of size 2 over the words of a line
* - emits each window of words to stdout
*
* usage: chopchop [-w 2]
*
@lindig
lindig / dir.ml
Last active May 9, 2024 04:54
OCaml - list recursively regular files in a directory
(** [dir_is_empty dir] is true, if [dir] contains no files except
* "." and ".."
*)
let dir_is_empty dir =
Array.length (Sys.readdir dir) = 0
(** [dir_contents] returns the paths of all regular files that are
* contained in [dir]. Each file is a path starting with [dir].
@lindig
lindig / xs-dinghy.sh
Last active September 23, 2016 20:06
Run tests on XenServer by downloading and installing micro-kernel VM
#! /bin/sh
set -ex
VERSION="0.1.17"
GH="https://github.com/lindig"
VM="$GH/xen-test-vm/releases/download/$VERSION/test-vm.xen.gz"
KERNEL="xen-test-vm-${VERSION//./-}.xen.gz"
GUEST="/boot/guest"
IMG="$GUEST/$KERNEL"
(* Creating a directory with a given ownership and permissions looks
simple until you take closer look. Many things can fail: you might
not have the permissions to create or modify it, the desired groups
and owners might not exist.
This library tries to be systematic about it and de-composes the
complex operation (implemented in [mk]) into many small operations
that are stringed together in a monad: [>>=] sequences operations
and [//=] (read as: "or-else") recovers from a previous error.
@lindig
lindig / func.ml
Created October 7, 2016 08:38
OCaml Functors - small examples
==> func0.ml <==
module Debug = struct
let debug msg = Printf.eprintf "debug: %s\n" msg
let error msg = Printf.eprintf "error: %s\n" msg
end
let main () =
let argv = Array.to_list Sys.argv in