Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually, <code>x</code> is local to the function, so <code>setf</code> or <code>setq</code>ing it only changes the local variable <code>x</code>. </p> <p>To answer the second part of your question, there is another way to define a local variable other than let:</p> <pre><code>(funcall (lambda (var1 var2...) ... use var1 var2 ...)- val1 val2 ...) </code></pre> <p>In fact, <code>defun</code> could be implemented as </p> <pre><code>`(setf (symbol-function ,name) (lambda ,args ,@body)) </code></pre> <p>though all the implementations I've checked do more than that. </p> <p>Also, the <code>symbol-function</code> place allows you to do this:</p> <pre><code>(setf (symbol-function '+) (function -)) </code></pre> <p>though that's generally a bad idea.</p> <h2>Your second question</h2> <p>What's happening there is that <code>x</code> is local to the scope that contains the <code>defun</code>. It's <strong>not</strong> a global variable. What you're doing with that <code>defun</code> is creating a closure that "captures" all the lexically-scoped (not created with <code>defvar</code>/<code>defparameter</code>) variables in the surrounding scopes and holds on to them for the future. If you want to inspect the value of <code>x</code>, add another function</p> <pre><code>(defun get-x () x) </code></pre> <p>inside the <code>let</code>. To put it another way, <code>x</code> is local to the <code>let</code> that the function is in. The situation you've provided is similar to:</p> <pre><code>(let ((x 3)) (print x) (let ((y 7)) (print (list x y)) (setf x y)) (print x)) </code></pre> <p>except the inner <code>let</code> is replaced by a <code>defun</code> (which is a <code>lambda</code>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload