Quasiquote (i.e. backquote) is just a syntactic suger for runtime list construction.
It's a very useful one too. First off it gives you the obvious quick list construction, allowing you to quickly move from evaluation to quotation context.
For instance the code (cons (append (list a 'b) c) d))
can be written as `(,a b ,@c ,@d)
(unquote-splicing, denoted with ,@
is used to append the list resulting from an expression).
Second, it allows to very quickly trace debug a piece of code. Say you have the following code:
(define string-split
(lambda (s delim)
(reverse (car (fold-left
(lambda (p ch)
(let ((str-lst (car p))
(char-lst (cdr p)))
(if (char=? ch delim)
(if (null? char-lst)
(cons str-lst '())
(cons (cons (list->string (reverse char-lst)) str-lst) '()))
(cons str-lst (cons ch char-lst)))))
`(() ())
(append (string->list s) `(,delim)))))))
If you call it with the arguments "abc def"
and #\space
, you'll get an error from list->string
. It's hard to see what went wrong and where, but if you quasiquote the call to list->string
and unquote (using ,
) the argument char-lst
, it'll give you a good hint to get started.