Backquote Operator

Simple example of typical usage of the backquote operator in the LispLanguages:

  `(list ,(+ 1 2) a b ,c)

It produces something like:
   (list 3 a b <whatever-the-value-of-c-is>)

I.e. backquote is used for "mostly-constant" data. Only the parts marked with a comma (,) are evaluated as Lisp code, the rest is interpreted as a literal object. This is most useful in LispMacros.


Also there is comma-at (,@) which splices a list into the new one instead of including it as an element.

   (let ((a 'value-of-a)
         (b 'value-of-b)
         (c '(value of c)))
     `(a is ,a - b is ,b - c is ,c - ,@c - ,@c))

returns

   (A IS VALUE-OF-A - B IS VALUE-OF-B - C IS (VALUE OF C) - VALUE OF C - VALUE OF C)

and is really sugar for writing something like this:

   (let ((a 'value-of-a) 
         (b 'value-of-b)
         (c '(value of c))) 
     (list* 'a 'is a '- 'b 'is b '- 'c 'is c '- (append c (cons '- c))))


In PerlLanguage, CeeShell, and BourneShell, the backquote is used to capture the output of a shell command into (for example) a variable:

  $foo = `/usr/bin/foo bar baz`;

ClearCase command (under Unix) to check in all checked-out files in a given view (given here as another backquote operator)

 cleartool ci -c "Checking in all files" `cleartool lsco -cvi -avo -short` 

In recent years backquote is deprecated; dollar-paren is preferred, to avoid parsing problems that go all the way back to the invention of backquote in cshell in the 1970s. So this example would be:

  cleartool ci -c "Checking in all files" $(cleartool lsco -cvi -avo -short)

However my fingers still tend to use backquote automatically without consulting me. :-)

It's one less character to type, bow to the wisdom of your fingers.

Is dollar-paren POSIXally correct? The Plan 9 "rc" shell has similar syntax to allow nesting:

  ls -l `{find `{query-for-dirs blah blah} -type f}


EditText of this page (last edited January 29, 2004) or FindPage with title or text search