Why is `(,x) a shorthand for (cons x '())?

88 views Asked by At

I'm told that: `(,x) is a shorthand for (cons x '()).

I'm a bit confused because that's not documented anywhere.

Also if that is the case, what does `(((pea)) ,q) evaluate to?

And why is pea wrapped in two sets of parens?

1

There are 1 answers

0
Barmar On BEST ANSWER
`(thing1 thing2 thing3 ...)

is roughly equivalent to

(list `thing1 `thing2 `thing3 ...)

When an expression inside backticks is preceded by a comma, that cancels out the backtick, so

`(,x)

is equivalent to

(list x)

Since a list is a chain of pairs whose last cdr is the empty list, this is equivalent to

(cons x '())

In your second example

`(((pea)) ,q)

is equivalent to

(list '((pea)) q)

Basically, when you use backquote, you can treat it like an ordinary quoted expression, except that , "unquotes" the subexpression that follows it. So you convert it to calls to list and cons with the non-comma expressions quoted, and the comma expressions not quoted.

As for why it has two sets of parentheses, that depends on how this data is being used. They needed pea to be nested 2 levels deep for some reason.