[racket] Can you avoid voids in quasiquote?

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Tue Jan 28 15:28:12 EST 2014

Sam Tobin-Hochstadt wrote at 01/28/2014 02:22 PM:
> `(p "foo" "bar" ,@(if test-condition (list (do-something)) null))
>    

This pattern with ",@" is very common in doing nontrivial SXML.  One 
additional thing I do, especially if I have multiple people of varying 
levels of Racket experience potentially working on the code, is a 
convention of using the backquote (quasiquote) throughout the tree of 
many levels of SXML literal chunks alternating with imperative code.  I 
suspect that using quasiquote everywhere is less likely to lead to 
runtime error when someone modifies the code.

For example, I would do:

`(p "a" "b" ,@(if foo? `("c" ,(d) "e") '()) "f")

and *not* do:

`(p "a" "b" ,@(if foo? (list "c" (d) "e") '()) "f")

You might be able to imagine how a newbie who doesn't know Racket lists 
very well could get more confused trying to add to the second example 
than the first.  Backquote and comma are syntactic patterns that they 
just use for literal SXML.

Nowadays, I also sometimes use 
"http://www.neilvandyke.org/racket-html-template/", which does more 
static checking, does HTML-escaping of values like produced by the "d" 
procedure above, looks more imperative, catches erroneous writes to 
stdout (which would be a bug that would mess up CGI-style scripts at 
runtime), doesn't involve any scary-looking quasiquoting and unquoting, 
and does much of the HTML conversion at syntax expansion time:

(html-template
   (p "a"
      "b"
      (%write (and foo?
                   (html-template
                     "c"
                     (% (d))
                     "e")))
      "f"))

I use this template package with my SCGI package, and for writing HTML 
files to be served by conventional Web servers.

For use with the Racket Web Server, there is 
"http://www.neilvandyke.org/racket-web-server-xexp/".  (This is not 
optimal, and I have already added hooks into the template package to 
optimize its use with Racket Web server, but I haven't yet had the need 
to take advantage of the hooks.)

Neil V.


Posted on the users mailing list.