Skip to content

Instantly share code, notes, and snippets.

View lindig's full-sized avatar

Christian Lindig lindig

View GitHub Profile
@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
\documentclass[a4paper,12pt]{article}
\usepackage{fullpage}
\usepackage[latin1]{inputenc}
\usepackage{graphicx}
\usepackage{xspace}
% PDF stuff
\ifx\pdfoutput\undefined
\else
\pdfpagewidth=210mm
@lindig
lindig / dir.ml
Last active August 5, 2021 10:43
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].
(*
* ocamllex bible.mll && ocamlopt -o bible bible.ml
* ./bible < bible.txt
*)
{
module L = Lexing
let get = L.lexeme
let sprintf = Printf.sprintf
@lindig
lindig / basic-module-design.md
Last active April 29, 2019 09:45
Basic considerations when creating a module in OCaml

Basic Module Design

First-In-First-Out Stack

module FIFO : sig
  exception Empty

  type 'a t

val empty: 'a t

{
(* short names for important modules *)
module L = Lexing
module B = Buffer
type token =
| STR of string
| VAR of string
#! /usr/bin/env ruby
#
# Report code size by OCaml module in a native binary
#
require 'set'
require 'getoptlong'
class OCamlModule
include Comparable
module type Monad = sig
type 'a t
val return: 'a -> 'a t
val bind: 'a t -> ('a -> 'b t) -> 'b t
end
(*
* ocamlbuild -pkg unix assoc_timing.native
*
* When visiting all keys in an association list, when does it
* make sense to build a map first? According to this,
* building the map is amortised for list longer than 20 to 30
* elements.
*)
module Int = struct
@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