Memoize discrete functions (restricted to a finite domain) using arrays