# Naive Implementation of Automatic Memoisation

I wrote code to (naively) perform automatic memoisation. I tried to write it in a functional programming style, and so did not make use of any global variables.

## My Code

def naive_memoise(caches):
def memoise(f):
nonlocal caches
def mem_f(n):
nonlocal caches, f
if f not in caches:
caches[f] = {}
if n not in caches[f]:
caches[f][n] = f(n)
return caches[f][n]
return mem_f
return memoise


### Sample Usage

def fib(n):
if n in [0, 1]:
return n
return  fib(n - 2) + fib(n - 1)

mem = naive_memoise({})
fib = mem(fib)

• Is there any use case in which you would want to initialize the cache with something other than an empty dict? – 200_success Mar 3 at 19:05

• the nonlocal are not required here
• in the code sample, you could use the @decorator syntax
• I could change the parameter of mem_f to *lst I guess so it could handle functions with multiple arguments. It would requires rewriting the functions that work with it though. Not sure I know a general way to do it without customising the functions to be memoised so they interface well with the memoisation closure. – Tobi Alafin Mar 2 at 23:39