let factory_add_memory = fun f -> fun n ->
if n > 0 then
(
let memory = Hashtbl.create n in
let new_fun = fun i ->
try Hashtbl.find memory i
with Not_found ->
let result = f i in
let () = Hashtbl.replace memory i result in
let () = Hashtbl.remove memory (i - n) in
result
in
new_fun
)
else raise (Value_operation "memory length cannot be < 0." )