Skip to content

Instantly share code, notes, and snippets.

Avatar
🐪

Vladimir Keleshev keleshev

🐪
View GitHub Profile
View levenshtein.ml
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
for j = 0 to fill_bound do previous.(j) <- j done;
View Makefile
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
View cortege.ml
module Cortege0 = struct
type _ t =
| [] : unit t
| (::) : 'a * 'b t -> ('a -> 'b) t
end
View pp_list.ml
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 = [
View base_vs_stdlib.md

Comparision of JaneStreet Base and OCaml Stdlib

How to use Base

Either use Base selectively:

# #show List.hd;;
View verboseRegExp.js
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, '');
View fusion.ml
open Printf
module Syntax = struct
type t =
| Unit
| Boolean of bool
| Number of int
| Name of string
| Divide of t * t
View Lexer.l
%{
#include <stdio.h>
#include "parser.h"
%}
%%
[ \r\n\t]* { continue; /* Skip blanks. */ }
[0-9]+ { sscanf(yytext, "%d", &yylval->value);
return TOKEN_NUMBER; }
View map-as-a-recursion-scheme.ml
(*
* This file can be executed by running:
* $ ocaml -rectypes map-as-a-recursion-scheme.ml
*
*)
open Printf
(*
View advanced_error_handling.ml
(* https://keleshev.com/advanced-error-handling-in-ocaml *)
let failwithf f = Printf.ksprintf failwith f
(*
t -> bool | int
e -> true | false
| 0 | 1 | 2 | …