SPLICE-UNQUOTE
Splice-unquote form expects one argument. Like unquote, it is ignored if it appears outside of backquoted list. Under backquoted list it works just like unquote, with the difference when the unquoted item evaluates to list. In that case the elements of the list are spliced into the containing backquoted list. The splice-unqoute has the special character ,@
(comma-at).
We have seen following example for UNQUOTE
:
>`(a ,(list 1 2) c) ; function LIST produces list (A (1 2) C) ; list (1 2) as sublist
With SPLICE-UNQUOTE
, we will get this:
>`(a ,@(list 1 2) c) (A 1 2 C) ; list (1 2) is spliced-in
The elements of the spliced-in sublist are freshly consed, which means that when we are splicing an existing list, it will not be modified. Since version 1.2.9.1 we can also splice cyclic or dotted lists: In the freshly consed copy, for cyclic lists it will break the cycle, and for dotted lists simply ignores the danglig atom.
>(setq b (list 'c 'd)) ; setting variable b to be list (C D) ; this is it >`(a ,@b e f) (A C D E F) ; elements of list (C D) spliced-in >b (C D) ; B has still the original list, without the E F tail
Note that when SPLICE-UNQUOTE
is used outside of backqouted list, it will be ignored, the expression evaluated, but it will not splice:
>(list 1 (list 2 3) 4) ; normal calls to LIST function (1 (2 3) 4) ; list (2 3) is sublist >(list 1 ,(list 2 3) 4) ; with comma outside of backqoute .. (1 (2 3) 4) ; .. just gives the same result >(list 1 ,@(list 2 3) 4) ; and comma-at outside of backqoute .. (1 (2 3) 4) ; .. same result, will not splice
The forms BACKQUOTE
, UNQUOTE
and SPLICE-UNQUOTE
are useful for writing macros.