Skip to content

Instantly share code, notes, and snippets.

View keleshev's full-sized avatar
🐪

Vladimir Keleshev keleshev

🐪
View GitHub Profile
(* Pretty-printing in OCaml: A Format Primer
https://keleshev.com/pretty-printing-in-ocaml-a-format-primer *)
let printf = Format.printf
let fprintf = Format.fprintf
let sprintf = Format.asprintf
let pp_string ppf string = fprintf ppf "%S" string
let pp_print_list ~sep pp_item =
ast.exe: ast.c
gcc -Werror -Wswitch -Wimplicit-fallthrough ast.c -o ast.exe
clean:
rm -f *.o *.exe *.s a.out
test.s: ast.exe
./ast.exe > test.s

Comparision of JaneStreet Base and OCaml Stdlib

How to use Base

Either use Base selectively:

# #show List.hd;;
let distance' ~bound a b =
let n = String.length a and m = String.length b in
assert (n >= m);
if n - m > bound then None else
let fill_bound = min m bound in
let previous = Array.make (m + 1) max_int in
module Cortege0 = struct
type _ t =
| [] : unit t
| (::) : 'a * 'b t -> ('a -> 'b) t
end
let printf = Format.printf
let fprintf = Format.fprintf
let dprintf = Format.dprintf
let pp_list ~sep pp =
Format.pp_print_list ~pp_sep:(fun ppf () -> Format.fprintf ppf sep) pp
let pp_string ppf string = fprintf ppf "%S" string
let many = [
function verboseRegExp(input) {
if (input.raw.length !== 1) {
throw Error('verboseRegExp: interpolation is not supported');
}
let source = input.raw[0];
let regexp = /(?<!\\)\s|[/][/].*|[/][*][\s\S]*[*][/]/g;
let result = source.replace(regexp, '');
open Printf
module Syntax = struct
type t =
| Unit
| Boolean of bool
| Number of int
| Name of string
| Divide of t * t
%{
#include <stdio.h>
#include "parser.h"
%}
%%
[ \r\n\t]* { continue; /* Skip blanks. */ }
[0-9]+ { sscanf(yytext, "%d", &yylval->value);
return TOKEN_NUMBER; }
(*
* This file can be executed by running:
* $ ocaml -rectypes map-as-a-recursion-scheme.ml
*
*)
open Printf
(*