About single quote, and double parentheses

Discussion of Common Lisp

About single quote, and double parentheses

Postby Dat Woo » Sat Nov 17, 2018 7:13 pm

I'm reading "Common Lisp Recipes", within the book, there is an example as below:

Please explain to me:
- why do we need a single quote before (let ...), as in : '(let ((temp, var-1)) )
- and, why do we need double Parentheses after let, as in: let ((temp,var-1))
- and, why there is a single comma after setf, as in: setf ,var-1, var-2

I had read document online about usage of single quote, and parentheses but the information they provided could not help me to explain the usage in this situation.

Below is the code:
Code: Select all
(defmacro swap (var-1 var-2)
  '(let ((temp, var-1))
    (setf ,var-1, var-2
          ,var-2 temp)
          (values) ))

Dat Woo
 
Posts: 2
Joined: Sat Nov 17, 2018 6:59 pm

Re: About single quote, and double parentheses

Postby pjstirling » Wed Nov 21, 2018 7:22 am

I think you've mis-copied your example, because

a) Macros usually use back-quote/tilde (`) rather than single-quote ('), because
b) You can't use comma outside of back-quote, and your example includes commas, as you noted

Comma is never used as a separator in Common-Lisp, instead it is integral to the way that back-quote works, your example should instead have been written like so, because comma is associated with the form after it, not the one before it:

Code: Select all
(defmacro swap (var-1 var-2)
  `(let ((temp ,var-1))
     (setf ,var-1 ,var-2
           ,var-2 temp)
     (values)))


A macro is a function that returns a FORM, which is usually, (but not always) a tree of LISTs, back-quote is a convenient templating system for generating such forms. Most of a back-quote template is literals, but forms inside a back-quote that are prefixed with a comma are evaluated in the context of the back-quote form itself.

The code equivalent to your back-quote form looks like so:

Code: Select all
(list 'let
      (list (list 'temp var-1))
      (list 'setf var-1 var-2
            var-2 'temp)
      (list 'values))


Which was a) annoying to type, b) difficult to read to see what its RESULT looks like because it doesn't follow the normal rules of indentation for the LET form.

Variables declared with LET can be initialised with a value, but they don't have to be (in which case they are implicitly initialised to NIL). Since the compiler cannot read your mind to learn your intention there needed to be a syntactic way to indicate which way is which, so if you want to give a initial value, you supply LET a list of (NAME VALUE), and if you don't, then you just provide the SYMBOL naming the variable. The outer set of parentheses are there to indicate the start and end of the variables to declare, because LET can create any number of variables between 0 and whatever your computer can copy with memory-wise.
pjstirling
 
Posts: 159
Joined: Sun Nov 28, 2010 4:21 pm

Re: About single quote, and double parentheses

Postby Dat Woo » Sun Nov 25, 2018 9:07 pm

Thank you pjstirling,

You response had helped me a lot, especially expand the format of let into (list 'let...)
Dat Woo
 
Posts: 2
Joined: Sat Nov 17, 2018 6:59 pm


Return to Common Lisp

Who is online

Users browsing this forum: Majestic-12 [Bot] and 10 guests