3

One of the properties of functional languages is that functions do not have side effects, thus the same input should always yield the same output. It seems that those languages could easily and heavily benefit from memoization.

But, at least regarding Erlang, there is no default memoization of the functions' calls. Is there any particular reason that Erlang (and other functional languages as far as I know) do memoize by default (or with a simple trigger), or at least have explicit, good support of memoization?

Is there something inherently wrong with memoization?

One reason I can imagine is that with memoization your memory footprint can grow quickly. Right, but Erlang already runs on VM and manages memory, so I guess it could tame caches and prevent them from growing quite easily.

Related:

Edit:

Community
  • 1
  • 1
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
  • 5
    Erlang is not side-effect free. For example, I could write a function that opens a logfile and returns the requested number of bytes from the end of it. Called twice with the same argument, this function will probably return two different values. Or consider random:uniform, whose return value cannot be cached. – Chris Aug 13 '13 at 15:59
  • Of course, we don't live in no-size-effect utopia. Still, lack of side effects is a cornerstone of FP – Jakub M. Aug 13 '13 at 16:11
  • 1
    Don't confuse side-effects with idempotentence. "Multiple executions with same input always yield same output" is the definition of idempotence, side-effects obviously does not affect output – user Aug 13 '13 at 16:13
  • 3
    The `too_big_to_fail` flag should of course not be taken seriously... April fools' day ;) – johlo Aug 13 '13 at 17:17
  • 1
    Holy Byte, they got me. Did anyone tell Motorola and Ericsson? :) – Jakub M. Aug 13 '13 at 17:50
  • Is there some stat that shows that there is an interest to do that. My first feeling is that it would be a huge design effort for a small effect. But it is only a feeling, and I am curious to know if there is any study done on a real life application. – Pascal Aug 13 '13 at 18:13

1 Answers1

5

There are a number of mistaken assumptions in this question.

One of the properties of functional languages is that functions do not have side effects

Incorrect, only "purely functional languages" have such a constraint. Erlang is not purely functional. It permits arbitrary side effects in functions.

Is there any particular reason that Erlang (and other functional languages as far as I know) do memoize by default (or with a simple trigger), or at least have explicit, good support of memoization?

No languages (at least non-toy ones) implement by-default memoization of all function calls. Why? Massive space leaks would ensue.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468