Skip to content

Instantly share code, notes, and snippets.

module rec Value : sig
type t =
| Int of int
| Tbl of t ValueTbl.t
val hash : t -> int
val equal : t -> t -> bool
end = struct
type t =
| Int of int
| Tbl of t ValueTbl.t
@gsg
gsg / scope.ml
Created February 17, 2017 04:29
type var = string
module Term = struct
type t =
| Int of int
| Lam of var * t
| Var of var
| App of t * t
end
@gsg
gsg / test.ml
Created October 30, 2016 11:50
type xy_part = [`X of int | `Y of bool]
type pre = [xy_part | `Pcon of pre * pre];;
let rec pcon_to_lstm (arg: pre) : xy_part list =
match arg with
| `Pcon (a, b) -> (pcon_to_lstm a) @ (pcon_to_lstm b)
| (#xy_part as x) -> [x]
@gsg
gsg / test.ml
Created October 30, 2016 11:49
type x_part = [`X of int]
type pre = [x_part | `Pcon of pre * pre];;
let rec pcon_to_lstm (arg: pre) : ([`X of int]) list =
match arg with
| `Pcon (a, b) -> (pcon_to_lstm a) @ (pcon_to_lstm b)
| (#x_part as x) -> [x]
@gsg
gsg / test.ml
Created October 30, 2016 10:35
let rec filter_map f = function
| [] -> []
| x::xs ->
match f x with
| Some y -> y::filter_map f xs
| None -> filter_map f xs
let list =
filter_map (function `G a -> Some (`G a) | _ -> None)
[`G 0; `F "wat"]
@gsg
gsg / test.ml
Created October 30, 2016 10:28
let filter xs =
List.filter (function `G a -> true | _ -> false) xs
let rec refine (xs : [> `G of 'a] list) : [`G of 'a] list =
match xs with
| [] -> []
| ((`G _) as elt)::rest -> elt::refine rest
| _::_ -> assert false
let argtest = [`X(`True);`G(`False)];;
╭───┬───╮ ╭───┬───╮ ╭───┬───╮
│ x │ ╶────► y │ ╶────► z │ │
╰───┴───╯ ╰─▲─┴───╯ ╰───┴───╯
╭───┬─│─╮
│ q │ ╵ │
╰───┴───╯
@gsg
gsg / test.ml
Created September 18, 2016 10:18
type instruction =
| Order of {id: int; price: float; size: int}
| Cancel of {id: int}
| Cancel_replace of {id: int; new_price: float; new_size: int}
let filter_by_oid instructions oid =
List.filter (function
| Order o -> o.id = oid
| Cancel c -> c.id = oid
| Cancel_replace cr -> cr.id = oid)
let most_significant_bit =
(-1) lxor ((-1) lsr 1)
let bprint_int_binary buf n =
Buffer.add_string buf "0b";
let rec loop started bit n =
if bit = 0 then begin
if not started then Buffer.add_char buf '0'
end
else
let most_significant_bit =
max_int lxor (max_int lsr 1)
let print_int_binary n =
print_string "0b";
let rec loop started bit n =
if bit = 0 then ()
else
let b = n land bit in
if b = 0 then begin