title: Common Lisp scoping (dynamic vs lexical)
body: <p>EDIT: I changed the example code after the first answer because I came up with a simple version that begs the same questions.</p>
  
  <p>I am currently learning Common Lisp's scoping properties. After I thought I had a solid understanding I decided to code up some examples that I could predict the outcome of, but apparently I was wrong. I have three question, each one relating to an example below:</p>
  
  <p>Example 1:</p>
  
  <pre><code>(defmethod fun1 (x)
    (print x)
    (fun2))
  
  (defmethod fun2 ()
    (print x))
  
  (fun1 5)
  </code></pre>
  
  <p>Output: </p>
  
  <pre><code>5 
  *** - EVAL: variable X has no value
  </code></pre>
  
  <p>Question: This makes sense. x is statically scoped and fun2 has no way of finding the value of x without having it passed explicitly.</p>
  
  <p>Example 2:</p>
  
  <pre><code>(defvar x 100)
  
  (defmethod fun1 (x)
    (print x)
    (fun2))
  
  (defmethod fun2 ()
    (print x))
  
  (fun1 5)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>5
  5
  </code></pre>
  
  <p>Question: I don't understand why x is suddenly visible to fun2 with the value that fun1 gave it, instead of having a value of 100...</p>
  
  <p>Example 3:</p>
  
  <pre><code>(setf x 100)
  
  (defmethod fun1 (x)
    (print x)
    (fun2))
  
  (defmethod fun2 ()
    (print x))
  
  (fun1 5)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>5
  100
  </code></pre>
  
  <p>Question: Should I ignore these results since calling setf on an undeclared variable is apparently undefined? This happens to be what I would expect in my second example...</p>
  
  <p>Any insight would be greatly appreciated...</p>