How do I write a generic memoize function?

后端 未结 15 1699
情话喂你
情话喂你 2020-12-09 05:25

I\'m writing a function to find triangle numbers and the natural way to write it is recursively:

function triangle (x)
   if x == 0 then return 0 end
   retu         


        
15条回答
  •  無奈伤痛
    2020-12-09 06:20

    Mathematica has a particularly slick way to do memoization, relying on the fact that hashes and function calls use the same syntax:

    triangle[0] = 0;
    triangle[x_] := triangle[x] = x + triangle[x-1]
    

    That's it. It works because the rules for pattern-matching function calls are such that it always uses a more specific definition before a more general definition.

    Of course, as has been pointed out, this example has a closed-form solution: triangle[x_] := x*(x+1)/2. Fibonacci numbers are the classic example of how adding memoization gives a drastic speedup:

    fib[0] = 1;
    fib[1] = 1;
    fib[n_] := fib[n] = fib[n-1] + fib[n-2]
    

    Although that too has a closed-form equivalent, albeit messier: http://mathworld.wolfram.com/FibonacciNumber.html

    I disagree with the person who suggested this was inappropriate for memoization because you could "just use a loop". The point of memoization is that any repeat function calls are O(1) time. That's a lot better than O(n). In fact, you could even concoct a scenario where the memoized implementation has better performance than the closed-form implementation!

提交回复
热议问题