Jorge Robla
Jorge Robla

Reputation: 9

Question about two expressions in Common Lisp - backquote and backtick

Why does 1 work ? why does not 2 work?

  1. (defun exprbis (&rest xs) `(+ (* 2 ,@xs) 1))
    EXPRBIS
    
  2. (defun exprbis (&rest xs) '(+ (* 2 ,@xs) 1))
    debugger invoked on a SB-INT:SIMPLE-READER-ERROR in thread
    #<THREAD "main thread" RUNNING {1001834103}>: Comma not inside a backquote.....
    

Thank you Guys!

Upvotes: 0

Views: 64

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139381

quote and backquote are two very different things.

quote is a special operator in Common Lisp

(quote (a b)) creates a literal constant list (a b). The quote character is an abbreviation. (quote (a b)) can be written as '(a b). quote does not introduce further syntax and so doesn't the quote character. A comma for example has no meaning there and thus will be a syntax error.

The backquote is special syntax of the Lisp reader. Inside the backquoted form there is special syntax for the comma character.

`(a ,b)

means to create a list with two elements: the symbol a and the value of the variable b.

`(a ,@b)

means to create a list with elements: the symbol a and the value, which needs to be a list, of the variable b spliced in.

Summary: the comma has no use inside a quote expression, which is a literal constant.

Upvotes: 3

Related Questions