title: Trying to understand setf + aref "magic"
body: <p>I now have learnt about arrays and <code>aref</code> in Lisp. So far, it's quite easy to grasp, and it works like a charme:</p>
  
  <pre><code>(defparameter *foo* (make-array 5))
  (aref *foo* 0) ; =&gt; nil
  (setf (aref *foo* 0) 23)
  (aref *foo* 0) ; =&gt; 23
  </code></pre>
  
  <p>What puzzles me is the <code>aref</code> "magic" that happens when you combine <code>aref</code> and <code>setf</code>. It seems as if <code>aref</code> knew about its calling context, and would then decide whether to return a value or a place that can be used by <code>setf</code>.</p>
  
  <p>Anyway, for the moment I just take this as granted, and don't think about the way this works internally too much.</p>
  
  <p>But now I wanted to create a function that sets an element of the <code>*foo*</code> array to a predefined value, but I don't want to hardcode the <code>*foo*</code> array, instead I want to hand over a place:</p>
  
  <pre><code>(defun set-23 (place)
    …)
  </code></pre>
  
  <p>So basically this function sets <em>place</em> to 23, whatever place is. My initial naive approach was</p>
  
  <pre><code>(defun set-23 (place)
    (setf place 23))
  </code></pre>
  
  <p>and call it using:</p>
  
  <pre><code>(set-23 (aref *foo* 0))
  </code></pre>
  
  <p>This does not result in an error, but it also doesn't change <code>*foo*</code> at all. My guess would be that the call to <code>aref</code> resolves to <code>nil</code> (as the array is currently empty), so this would mean that</p>
  
  <pre><code>(setf nil 23)
  </code></pre>
  
  <p>is run, but when I try this manually in the REPL, I get an error telling me that:</p>
  
  <blockquote>
    <p>NIL is a constant, may not be used as a variable</p>
  </blockquote>
  
  <p>(And this absolutely makes sense!)</p>
  
  <p>So, finally I have two questions:</p>
  
  <ol>
  <li>What happens in my sample, and what does this not cause an error, and why doesn't it do anything?</li>
  <li>How could I solve this to make my <code>set-23</code> function work?</li>
  </ol>
  
  <p>I also had the idea to use a thunk for this to defer execution of <code>aref</code>, just like:</p>
  
  <pre><code>(defun set-23 (fn)
    (setf (funcall fn) 23))
  </code></pre>
  
  <p>But this already runs into an error when I try to define this function, as Lisp now tells me:</p>
  
  <blockquote>
    <p>(SETF FUNCALL) is only defined for functions of the form #'symbol.</p>
  </blockquote>
  
  <p>Again, I wonder why this is. Why does using <code>setf</code> in combination with <code>funcall</code> apparently work for named functions, but not for lambdas, e.g.?</p>
  
  <p>PS: In "Land of Lisp" (which I'm currently reading to learn about Lisp) it says:</p>
  
  <blockquote>
    <p>In fact, the first argument in <code>setf</code> is a special sublanguage of Common Lisp, called a <em>generalized reference</em>. Not every Lisp command is allowed in a generalized reference, but you can still put in some pretty complicated stuff: […]</p>
  </blockquote>
  
  <p>Well, I guess that this is the reason (or at least one of the reasons) here, why all this does not work as I'd expect it, but nevertheless I'm curious to learn more :-)</p>