I'm learning common lisp and I have a problem with understanding the usage of two backquotes combined with two commas:
``(a ,,(+ 1 2))
I mean, I don't have a clue why it's evaluated to:
`(A ,3)
rather than something like that:
`(A 3)
I'm explaining myself that both commas were 'consumed' in order to evaluate two backquotes in front of the form so none of the commas should've left and yet there's still one. How would look
``(a ,,(+ 1 2))
using only list and ' ?
From the specs
This is what the Common Lisp HyperSpec says about nested backticks:
The R5RS Scheme spec also includes these details about backticks:
Also keep in mind that only one backtick gets collapsed per evaluation, just like a regular quote, it's not recursive.
Rules in action
To see how these three details interact, let's expand your example a bit. This expression...
Gets evaluated to this (in SBCL notation):
(+ 1 2)
got escaped by the matching comma (the 2nd comma, according to the HyperSpec).(+ 3 4)
didn't have enough commas to get expanded (which is what R5RS mentions).Expanding both commas
To get rid of the other backtick, another level of evaluation is needed:
Both backticks are gone, and we're left with a plain list:
No, both commas were consumed. There were two levels of quoting and two levels of commas. Now there's one level of quoting and one level of commas. In fact, GNU Common Lisp (2.44.1) evaluates your expression as
That's exactly the same thing as
but more explicitly has "evaluated" both commas.