title: Memoized recursive functions. How to make them fool-proof?
body: <p>Memoized functions are functions which remember values they have found.
  Look in the doc center for some background on this in Mathematica, if necessary.</p>
  
  <p>Suppose you have the following definition</p>
  
  <pre><code>f[0] = f[1] = 1
  f[x_] := f[x] = f[x - 1] + f[x - 2]
  </code></pre>
  
  <p>in one of your packages. A user may load the package and start asking right away f[1000].
  This will trigger a $RecursionLimit::reclim error message and abort.
  Even if the user then tries something smaller, say f[20], by now the definition of f is corrupt and the result is not good anymore.Of course the package developer might increase the recursion limit and warn the user, but my question is:</p>
  
  <p>How can you improve the f definition so that if the user asks for f[1000] he/she gets the answer without any problem? I am interested in a way to trap the user input, analyze it and take whatever steps are necessary to evaluate f[1000].</p>
  
  <p>I can easily imagine that one can change the recursion limit if the input is more than 255 (and then bring it back to the original level), but what I would really like to see is, if there is a way for the f to find out how many values it "knows" (fknownvalues) and accept any input &lt;=fknownvalues+$RecursionLimit without problems or increase the $RecursionLimit if the input is higher.</p>
  
  <p>Thank you for your help</p>