Instantly share code, notes, and snippets.

# Drup/difflist.ml Last active Aug 29, 2017

Difference lists and Miniformat
 type ('ty,'v) t = | Nil : ('v, 'v) t | Cons : 'a * ('ty, 'v) t -> ('a -> 'ty, 'v) t let cons x l = Cons (x,l) let plus1 l = Cons ((),l) let one x = Cons (x,Nil) let l1 = Cons (1, Cons ("bla", Nil)) let l2 = Cons ((), Cons (2., Nil)) let l3 = Cons (1, Cons ("bla", Cons ((), Cons(2.,Nil)))) let rec append : type a b c. (a, b) t -> (b, c) t -> (a, c) t = fun l1 l2 -> match l1 with | Nil -> l2 | Cons (h, t) -> Cons (h, append t l2)
 type ('ty,'v) t = | End : ('v,'v) t | Constant : string * ('ty,'v) t -> ('ty,'v) t | Hole : ('ty, 'v) t -> (string -> 'ty, 'v) t ;; let rec kprintf : type ty res. (string -> res) -> (ty,res) t -> ty = fun k -> function | End -> k "" | Constant (const, fmt) -> kprintf (fun str -> k @@ const ^ str) fmt | Hole fmt -> let f s = kprintf (fun str -> k @@ s ^ str) fmt in f ;; let printf fmt = kprintf (fun x -> x) fmt ;; (* "%s | %s" *) let ex = Hole (Constant (" | ", Hole End)) ;; printf ex "foo" "zoo" ;;
to join this conversation on GitHub. Already have an account? Sign in to comment