title: Why is this Haskell code snippet not infinitely recursive?
body: <p>To help me learn Haskell, I am working through the problems on Project Euler. After solving each problem, I check my solution against the Haskell wiki in an attempt to learn better coding practices. Here is <a href="http://www.haskell.org/haskellwiki/Euler_problems/1_to_10#Problem_3">the solution</a> to <a href="http://projecteuler.net/index.php?section=problems&amp;id=3">problem 3</a>:</p>
  
  <pre><code>primes = 2 : filter ((==1) . length . primeFactors) [3,5..]
  
  primeFactors n = factor n primes
    where
      factor n (p:ps) 
          | p*p &gt; n        = [n]
          | n `mod` p == 0 = p : factor (n `div` p) (p:ps)
          | otherwise      = factor n ps
  
  problem_3 = last (primeFactors 317584931803)
  </code></pre>
  
  <p>My naive reading of this is that <code>primes</code> is defined in terms of <code>primeFactors</code>, which is defined in terms of <code>primes</code>. So evaluating <code>primeFactors 9</code> would follow this process:</p>
  
  <ol>
  <li>Evaluate <code>factor 9 primes</code>.</li>
  <li>Ask <code>primes</code> for its first element, which is 2.</li>
  <li>Ask <code>primes</code> for its next element.</li>
  <li>As part of this process, evaluate <code>primeFactors 3</code>.</li>
  <li>Ask <code>primes</code> for its first element, which is 2.</li>
  <li>Ask <code>primes</code> for its next element.</li>
  <li>As part of this process, evaluate <code>primeFactors 3</code>.</li>
  <li>...</li>
  </ol>
  
  <p>In other words, steps 2-4 would repeat infinitely. Clearly I am mistaken, as the algorithm terminates. What mistake am I making here?</p>