backquote, unquote and unquote-splicing in normal functions

2.7k Views Asked by At

I am still in the process of understanding macros and though I think I understand the basics of "backquote" "unquote" and "unquote splicing", I had thought they were only used/useful in macros.

however I came across this Common Lisp code from Rosetta code (the calender task),

(defun month-strings (year month)
  "Collect all of the strings that make up a calendar for a given
    MONTH and YEAR."
  `(,(date-calc:center (month-to-word month) (length *day-row*))
    ,*day-row*
     ;; We can assume that a month calendar will always fit into a 7 by 6 block
     ;; of values. This makes it easy to format the resulting strings.
      ,@ (let ((days (make-array (* 7 6) :initial-element nil)))
           (loop :for i :from (date-calc:day-of-week year month 1)
                 :for day :from 1 :to (date-calc:days-in-month year month)
                :do (setf (aref days i) day))
           (loop :for i :from 0 :to 5
             :collect
             (format nil "~{~:[  ~;~2,d~]~^ ~}"
                 (loop :for day :across (subseq days (* i 7) (+ 7 (* i 7)))
                  :append (if day (list day day) (list day))))))))

here back-quote, unquote, and unquote splicing are used in a normal function, and it works to create a list of strings.

and while I do not use Scheme, the Racket solution had something similar,

(define days
    (let ([? (if (= mn 12) (λ(x y) y) (λ(x y) x))])
      (round (/ (- (find-seconds 0 0 12 1 (? (+ 1 mn) 1) (? yr (+ 1 yr))) s)
                60 60 24))))
  (list* (~a mname #:width 20 #:align 'center) "Su Mo Tu We Th Fr Sa"
         (map string-join
              (nsplit 7 `(,@(make-list pfx "  ")
                          ,@(for/list ([d days])
                              (~a (+ d 1) #:width 2 #:align 'right))
                          ,@(make-list (- 42 pfx days) "  ")))))))

which I did not test.

my questions are,

Why would this be necessary in a function, what is the use case?

and how is it different from a macro ?

3

There are 3 best solutions below

11
Sylwester On

quasiquote, unquote and unquote-splicing are just syntax sugar for a combination of quoted datum, list, and cons. Imagine this:

`(,a b c)    ; == (cons a '(b c))
`(a b ,c)    ; == (list 'a 'b c)
`(a b ,@c d) ; == (cons 'a (cons 'b (append c '(d))))  

These are small trivial examples, so you can imagine the right hand side might become crazy complex, but it is nice to know that the quasiquote magic makes new cons when needed and keep literals as is in the tails. Thus using nconc of quasiquoted expression would not work in the first case, but in the second and third because the last cons is needed to be fresh in those cases.

If you have a function that creates a list structure, quasiquote will make the code more clear and consice since the form will look more like the result. It is no different than a macro since both create list structure. A macro differs in what happens with the result. In a function the value is returned and in a macro code gets replaced.

You can check what happens after using the macro with macroexpand:

(macroexpand '`(,a ,b ,@c))
; ==> (cons a (cons b c)) 
; ==> t
3
Eggcellentos On

Quasi-Quote (QQ) is a list constructor in Scheme.

It’s more flexible than quote ('), list or cons because it allows for mixing symbols with expression evaluations.

The QQ has two helper mechanisms:

  1. unquote - denoted (,)

  2. unquote-splicing - denoted (,@)

When QQ is used, a quote context is initiated. Unquote allows us to momentarily escape the quote context and evaluate the expression right after the unquote. Unquote-splicing both escapes an entire list from the quote context as well as “unwraps” the list. Consider:

(define b 5)
(define s (list 1 2))

Note the differences in the values of the following expressions: Quote, Quasi-Quote.

Input:

'(a b c)
`(a b c)

Output

> (a b c)    
> (a b c)

Input

'(a ,b c)    
`(a ,b c)

Output

> (a ,b c)   
> (a 5 c)

Input

'(a ,s c)    
`(a ,s c)

Output

> (a ,s c)   
> (a (1 2) c)

Input

'(a ,@s c)   
`(a ,@s c)

Output

> (a ,@s c)  
> (a 1 2 c)

Source: Compilation course I took , The Common Lisp Cookbook - Macros and Backquote

0
Rainer Joswig On

Backquote

See the CLHS on Backquote.

The example

The example is similar to this code:

CL-USER 14 > (let ((a 1)
                   (b 2)
                   (c '(3 4 5)))
               `(,a                ; a gets evaluated and put in
                 ,b                ; b gets evaluated and put in
                 ,@c))             ; c gets evaluated and spliced in
(1 2 3 4 5)

The effect of above code is similar to using the function list*.

CL-USER 15 > (let ((a 1)
                   (b 2)
                   (c '(3 4 5)))
               (list* a b c))     
(1 2 3 4 5)

Which version you use is mostly a matter of taste.

list* creates a list of the first values and puts them in front of the last value, which usefully should be a list.

List creation

There are many ways and styles to create a list. The two here:

  1. use nested functions like cons, list, list*, append, ... This is especially useful when there are many elements to be computed.
  2. use templates of lists with the backquote operator and ,, ,@, ,. for evaluating. This is especially useful when there are nested lists with fixed objects and a few objects to compute.

Thus the backquoted way is useful when you think of list templates to fill out. Whenever you want to create (nested) lists, which are based on templates with a lost of constant structure (meaning objects and nesting), then this is a way to do it. This is not limited to macros - it is a general mechanism to build lists.

You can also think of templates being an inversion:

  1. functions evaluate by default and constant elements need to be quoted
  2. backquote templates don't evaluate by default and variable elements need to be unquoted.

Warning

Backquoted expressions themselves don't need to be pure lists. The internal representation of backquoted expressions is undefined and implementations actually differ.

Vectors, too

Note that this works for vectors, too:

CL-USER 20 > (let ((a 1)
                   (b 2)
                   (c '(3 4 5)))
               `#(,a
                  ,b
                  ,@c))
#(1 2 3 4 5)