Generic Lisp, reference to value and actual value

Consider this piece of code:

(defvar lst '(1 1))

(defmacro get-x (x lst)
  `(nth ,x ,lst))

(defun get-y (y lst)
  (nth y lst))

Now suppose I want to change the value of the items in the lst list, the car with get-x and cdr with get-y. When I try to change the value using get-x (using setf), everything goes fine, but if I try it with get-y, it signals an error (abbreviated):

; caught STYLE-WARNING:; undefined: (SETF GET-STUFF)

Why is this happening?

I myself suspect that this is because the macro simply expands, and the nth function simply returns a reference to the value of the element in the list, and the function, on the other hand, evaluates the function call to nth and returns the value of the reference value (it sounds confusing).

Am I right in my suspicions? If I am right, then how do I know what is just a reference to the value and the actual value?

+1
2

, , , (setf (get-x some-x some-list) some-value) ( ) - (setf (nth some-x some-list) some-value) ( , setf -expansion ), , (.. setf, nth).

, get-y, setf, . :

(defun (setf get-y) (new-value x ls)    ; Note the function name: setf get-y 
    (setf (nth x ls) new-value))

, setf -expanders:

  • setf
  • setf ( , setf )

, BTW, , "" Common Lisp ( , ++), Lisp , . (.. setf ) -, C++. . CLHS, .

+8

SETF - .

, , (, , - ). SETF :

(get-something x)

. :

(setf (get-something x) :foobar)

X : FOOBAR.

SETF (get-something x) - . . GET-SOMETHING, , .

SETF ? : .

NTH SETF , n- . Common Lisp.

GET-Y SETF . . . Lisp HyperSpec. DEFUN (SETF GET-Y) .

:

  • lst DEFVAR. * list * , , , DEFVAR ( ).

  • '(1 2) - . Common Lisp, undefined. , - COPY-LIST.

+4

Source: https://habr.com/ru/post/1543674/


All Articles