why memoization is not a language feature?

后端 未结 11 611
无人及你
无人及你 2021-01-31 09:00

I was wondering... why memoization is not provided natively as a language feature by any language I know about?

Edit: to clarify, what I mean is that th

相关标签:
11条回答
  • 2021-01-31 09:15

    Not all the languages natively support function decorators. I guess it would be a more general approach to support rather than supporting just memoization.

    0 讨论(0)
  • 2021-01-31 09:17

    In order for memoization to work as a language feature there would be a couple requirements.

    1. The compiler would need to be identify valid functions for memoization (e.g. they are referentially transparent).
    2. The run-time would have to be able to intelligently select candidates for memoization without slowing down the overall performance.

    There are some assumptions in the other language, but if we can have performance gains by just-in-time compilation of hot-spots in a Java VM, then one can surely write an automated memoziation system.

    While non-trivial I think this is all theoretically possible to get performance gains in a language (especially an interpreted one) and is a worthwhile area for research.

    0 讨论(0)
  • 2021-01-31 09:20

    Your question also leaves open the solution of your learning more languages. I think that Lisp supports memoization, and I know that Mathematica does.

    0 讨论(0)
  • 2021-01-31 09:21

    In Haskell, memoization is automatic for (pure) functions you've defined that take no arguments. And the Fibonacci example in that Wiki is really about the simplest demonstrable example I would be able to think of either.

    Haskell can do this because your pure functions are defined to produce the same results every time; of course, monadic functions that depend on side effects won't be memoized.

    I'm not sure what the upper limits are -- obviously, it won't memoize more than the available memory. And I'm also not sure offhand if the memoization occurs at compile-time (if the values can be determined at compile-time), or if it always occurs the first time the function is called.

    0 讨论(0)
  • 2021-01-31 09:26

    Because you shouldn't implement something as a language feature when it can easily be implemented in the language itself. A memoization feature belongs in a library, which is exactly where most languages put it.

    0 讨论(0)
提交回复
热议问题