Character Resolution in the Lisp General List

Suppose I have a function

CL-USER> (defun trimmer (seq) "This trims seq and returns a list"
      (cdr 
         (butlast seq)))
TRIMMER
CL-USER> (trimmer '(1 2 3 VAR1 VAR2))
(2 3 VAR1)
CL-USER> 

Please note that due to QUOTE, VAR1 and VAR2 are not allowed. Suppose I want to allow the characters VAR1 and VAR2 to my values ​​- is there a standard function for this?

+3
source share
2 answers

Do not use quoteto create a list with variables; Use instead list:

CL-USER> (trimmer (list 1 2 3 var1 var2))
(2 3 value-of-var1)

(where value-of-var1is the value var1).

quote . , . , , list. backquote, .

+6

Backquote - :

> (setq var1 4 var2 5)
5
> `(1 2 3 ,var1 ,var2)
(1 2 3 4 5)

: , symbol-value, :

(defun interpolate-symbol-values (list)
  "Return a copy of LIST with symbols replaced by their symbol-value."
  (loop for x in list
        collect (if (symbolp x) (symbol-value x) x)))

> (interpolate-variables '(1 2 3 var1 var2))
(1 2 3 4 5)

, . , ? , .

+4

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


All Articles