title: How does reduce function work?
body: <p>As far as I understand, the reduce function takes a list <code>l</code> and a function <code>f</code>. Then, it calls the function <code>f</code> on first two elements of the list and then repeatedly calls the function <code>f</code> with the next list element and the previous result. </p>
  
  <p>So, I define the following functions:</p>
  
  <p>The following function computes the factorial. </p>
  
  <pre><code>def fact(n):
      if n == 0 or n == 1:
          return 1
      return fact(n-1) * n
  
  
  def reduce_func(x,y):
      return fact(x) * fact(y)
  
  lst = [1, 3, 1]
  print reduce(reduce_func, lst)
  </code></pre>
  
  <p>Now, shouldn't this give me <code>((1! * 3!) * 1!) = 6</code>? But, instead it gives <code>720</code>. Why <code>720</code>? It seems to take the factorial of <code>6</code> too. But, I need to understand why.</p>
  
  <p>Can someone explains why this happens and a work-around?</p>
  
  <p>I basically want to compute the product of factorials of all the entries in the list.
  The backup plan is to run a loop and compute it. But, I would prefer using reduce.</p>