memoize = function (f)
   local t = {}
      return function (x)
      if not t[x] then t[x] = f(x) end
      return t[x]
      end
   end
