This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

simple problem with setf

4 posts · 1728 views

Problem:
(defun proba1 (li) (first (second li)))

(defparameter li (list 1 (list 2) 3))

(setf (first (second li)) 1)

( setf (proba1 li) 1) ; result is an error
The question is, how can the last command cause an error, when the one before it does not, and it does the same thing (but trough a function) ?

Re: simple problem with setf

SETF is a macro which expands to the correct procedure for setting a place. There must be a valid setf expansion for it to work. Expansions for functions are not automatically generated, since that would not be correct in the majority of cases. You can create your own expander for functions in a number of ways, the simplest of which is DEFUNin a (setf ...) function, like this:
(defun (setf proba1) (new-value list) (setf (first (second list)) new-value))
Also, you have created a special variable with the same name as a function argument. You should never do that, since that reacts in ways which are often confusing, which is a reason for the convention of naming special variables with symbol names surrounded by stars.

Re: simple problem with setf

Thank you for the answer. My next question is, what is the general syntax of setf expander function definitions (google doesn't seem to help me with this) ?