Guest User

Untitled

a guest
Oct 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.44 KB | None | 0 0
  1. exception Chosen of string
  2.  
  3. (* h : (string, string list) Hashtbl.t *)
  4.  
  5. let choose h =
  6.   let tot = Hashtbl.fold (fun _ v s -> s + List.length v) h 0 in
  7.   try
  8.     ignore
  9.       (Hashtbl.fold
  10.         (fun k v r ->
  11.           let len = List.length v in
  12.           let t = r - len in
  13.           if t < 0 then raise (Chosen k)
  14.           else t)
  15.         h
  16.         (Random.int tot));
  17.     assert false (* not reached *)
  18.   with Chosen k ->
  19.     k
Add Comment
Please, Sign In to add comment