title: Variable in a function
body: <p>I have see the following code... The first call of <code>(next-num)</code> returns <code>1</code>, and the second returns <code>2</code>.</p>
  
  <pre><code>(define next-num
    (let ((num 0))
      (lambda () (set! num (+ num 1)) num)))
  
  (next-num)   ; 1
  (next-num)   ; 2
  </code></pre>
  
  <p>What I can not understand is... <code>num</code> is created by <code>let</code> inside <code>next-num</code>, it is kind of a local variable... How does scheme know that each time <code>next-num</code> is called, the value of <code>num</code> is not erased by <code>let ((num 0))</code>; How does scheme know that it is always the same <code>num</code> that we modify whenever <code>next-num</code> is called? </p>
  
  <p>It seems that <code>num</code> is both local and static... How can we define a local variable, but not static?</p>