5 ms·
By the way, don't confuse macros with backquote; the backquote facility is probably closer to C's preprocessors. I haven't heard this claim before, and I might
by mahmud 16y ago
By the way, don't confuse macros with backquote; the backquote facility is probably closer to C's preprocessors. I haven't heard this claim before, and I might not be entirely right, but it's what provides for the "fill in the blanks" type symbolic computing.
For example:
(defun sum (x y)
`(,x + ,y = ,(+ x y)))
This is a SUM function which looks like this when run:
(sum 4 5) ==> (4 + 5 = 9)
The backquote (`) says the following form and its sub-forms (if any) are all quoted. But you can poke holes in the quote and fill in with the value of outside variables, using comma (,). So in the example, everything is quoted in the body of sum, except three things: x, y, and (+ x y). Each of those is then evaluated yielding 4, 5 and 9, then, finally, the template is filled in (4 + 5 = 9).
If you want to confirm this, you can change the + and = to any other symbol and it would still display. E.g:
(defun sum (x y)
`(,x frob ,y != ,(+ x y)))
(sum 4 5) ==> (4 FROB 5 != 9)
- arethuza 16y agoYou can also splice values into backquoted expressions using ,@foo rather than ,foo - which I seem to remember being rather useful.
- zck 16y agoIt's incredibly useful. The actual definition of until from Arc: (mac until (test . body) `(while (no ,test) ,@body)) It's really useful for bodies of code like this.
- sedachv 16y agoDon't forget that `, (backquote-comma) can also be adapted for pattern matching/destructuring (http://www.cliki.net/fare-matcher http://www.cliki.net/fare-matcher does this). I think combining backquote-comma's s-exp-making and s-exp-destructuring powers is a viable way to abstract s-exps from their representations as linked lists/arrays/whatever sequence you want to use.
- wingo 16y agoI like Conrad Barski's take on explaining quasiquote: http://www.lisperati.com/looking.html http://www.lisperati.com/looking.html "Flip, flop, flip, flop..."