Last active
June 12, 2023 17:26
-
-
Save Drup/f8e1564c41374bf38e2cb74dfb0c857a to your computer and use it in GitHub Desktop.
Difference lists and Miniformat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment