About a generic variable in onlisp

I'm not sure what is going on here, a macro example in the text. In principle, it is inconvenient to use the get-setf method, a built-in macro (perhaps a function?). To be specific, how about some get-setf method return values ​​vanish? e.g. (get-setf-method 'x)

NIL ;
NIL ;
(#:NEW-3069) ;
(SETQ X #:NEW-3069) ;
X

And why does this sample code first set the fifth return value to the second return value for initialization? Finally, how can it handle the installation order of variables in an expression such as (aref ar (incf i)

(get-setf-method '(aref ar (incf i)))

(#:G3070 #:G3071) ;
(AR (INCF I)) ;
(#:G3072) ;
(SYSTEM::STORE #:G3070 #:G3071 #:G3072) ;
(AREF #:G3070 #:G3071)

Here is the macro definition:

(defmacro sortf (op &rest places)
  (let* ((meths (mapcar #'(lambda (p)
                             (multiple-value-list
                              (get-setf-method p)))
                        places))
         (temps (apply #'append (mapcar #'third meths))))
    `(let* ,(mapcar #'list
                     (mapcan #'(lambda (m)
                                  (append (first m)
                                          (third m)))
                             meths)
                     (mapcan #'(lambda (m)
                                  (append (second m)
                                          (list (fifth m))))
                             meths))
        ,@(mapcon #'(lambda (rest)
                       (mapcar
                        #'(lambda (arg)
                             `(unless (,op ,(car rest) ,arg)
                                 (rotatef ,(car rest) ,arg)))
                        (cdr rest)))
                  temps)
        ,@(mapcar #'fourth meths))))
+3
source share
1 answer

- . get-setf-method get-setf-expansion, SETF-METHOD-VS-SETF-METHOD Writeup. , , get-setf-expansion. , . , .

nil, get-setf-expansion , nil:

(get-setf-expansion 'x)
;=>  NIL, NIL, (#:G0001), (SETQ X #:G0001), X 

? :

:

get-setf-expand &optional

& Rightarrow; vars, vals, store-vars, writer-form, reader-form

:

-.

environment - .

vars, vals, store-vars, writer-form, reader-form - setf.

5.1.1.2 Setf:

, , , let *, , .

( , ), , .

, , , .

, , , , setf.

, .

, ?

(get-setf-expansion 'x)
;β‡’  NIL, NIL, (#:G0001), (SETQ X #:G0001), X 

x - , , . , , . - . , setf , . , . - (setq x #:g0001), , . x, , .

SBCL:

CL-USER> (defstruct person
           person-name)
;β‡’ PERSON

CL-USER> (get-setf-expansion '(char (person-name (first (second list-of-list-of-persons))) 3))
; (#:TMP965)
; ((PERSON-NAME (FIRST (SECOND LIST-OF-LIST-OF-PERSONS))))
; (#:NEW964)
; (SB-KERNEL:%CHARSET #:TMP965 3 #:NEW964)
; (CHAR #:TMP965 3)

, ,

(let* ((temp965 (person-name (first (second list-of-list-of-persons))))
       (old-char (char tmp965 3)))        ; optional 
  (setq new964 <compute-new-value>)
  (sb-kernel:%charset tmp965 3 new964))

, ( <compute-new-value>), , ( ). , , new964 , -, ​​.

" " get-setf-expansion:

+5

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


All Articles