title: Why is this global variable only able to be set twice, and no more?
body: <p>I was trying to make a simple parser in elisp, and I encountered a problem where I <code>defvar</code> a global, then later I <code>setq</code> a new value into it.  This works the first time.  However, later <code>setq</code>'s fail every time.</p>
  
  <p>The following code is a simplication of the problem:</p>
  
  <pre><code>(defvar buf '(BUF))
  (defvar head nil)
  
  (defun pparse (seq)
  
    (defun status ()
      (princ (format "Parse: %40s || %-20s\n"
             (prin1-to-string seq) (prin1-to-string buf))))
  
    (while seq
      (status)
      (setq head (car seq))
      (setq seq (cdr seq))
      (cond ((equal "x" head)
         (nconc buf (list head)))
        ((equal "," head)
         (setq buf '(BUF))
         ;;(setcdr buf nil) &lt;- fixes it but doesn't answer my question
        )))
  
    (status))
  
  (pparse '("x" "," "x" "," "x" "," "x"))
  </code></pre>
  
  <p>Which produces this output:</p>
  
  <pre><code>Parse:            ("x" "," "x" "," "x" "," "x") || (BUF)               
  Parse:                ("," "x" "," "x" "," "x") || (BUF "x")           
  Parse:                    ("x" "," "x" "," "x") || (BUF)               
  Parse:                        ("," "x" "," "x") || (BUF "x")           
  Parse:                            ("x" "," "x") || (BUF "x")           
  Parse:                                ("," "x") || (BUF "x" "x")       
  Parse:                                    ("x") || (BUF "x" "x")       
  Parse:                                      nil || (BUF "x" "x" "x")   
  </code></pre>
  
  <p>As you can see, the second column was clipped once, but grew every subsequent time.</p>
  
  <p>If you uncomment the <code>setcdr</code> line, this works as expected (output is as follows).  You can even dump the <code>setq</code>.  I understand why this fixes it, but not why the original error happens in the first place.</p>
  
  <pre><code>Parse:            ("x" "," "x" "," "x" "," "x") || (BUF)               
  Parse:                ("," "x" "," "x" "," "x") || (BUF "x")           
  Parse:                    ("x" "," "x" "," "x") || (BUF)               
  Parse:                        ("," "x" "," "x") || (BUF "x")           
  Parse:                            ("x" "," "x") || (BUF)               
  Parse:                                ("," "x") || (BUF "x")           
  Parse:                                    ("x") || (BUF)               
  Parse:                                      nil || (BUF "x")           
  </code></pre>
  
  <p>BTW, the behavior is the same even if i turn off lexical scoping.</p>