let memo f = let cache = Hashtbl.create 103 in fun x -> try Hashtbl.find cache x with Not_found -> let res = f x in (Hashtbl.add cache x res; res)