title: Haskell function definition and caching arrays
body: <p>I have a question about implementing caching (memoization) using arrays in Haskell.  The following pattern works:</p>
  
  <pre><code>f = (fA !)
    where fA = listArray...
  </code></pre>
  
  <p>But this does not (the speed of the program suggests that the array is getting recreated each call or something):</p>
  
  <pre><code>f n = (fA ! n)
    where fA = listArray...
  </code></pre>
  
  <p>Defining fA outside of a where clause (in "global scope") also works with either pattern.</p>
  
  <p>I was hoping that someone could point me towards a technical explanation of what the difference between the above two patterns is.</p>
  
  <p>Note that I am using the latest GHC, and I'm not sure if this is just a compiler peculiarity or part of the language itself.</p>
  
  <p>EDIT: ! is used for array access, so fA ! 5 means fA[5] in C++ syntax.  My understanding of Haskell is that (fA !) n would be the same as (fA ! n)...also it would have been more conventional for me to have written "f n = fA ! n" (without the parentheses).  Anyway, I get the same behaviour no matter how I parenthesize.</p>