[racket] #<void> in xexpr

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Sat Dec 10 01:30:28 EST 2011

Jordan Schatz wrote at 12/10/2011 01:12 AM:
[...]
>                       ,(when autofocus
>                            '(autofocus "autofocus")))))))
>
> But using "when" produces #<void>  like so:
>    

This is a very common thing to need to do when generating XML and HTML.  
The trick is to use ",@(if" instead of ",(when":

,@(if autofocus
       '((autofocus "autofocus"))
       '())

Note that this example also introduced an extra set of parentheses 
around the xexpr parts to be inserted.  The ",@" is saying ``at this 
position, splice in the contents of the following list of 0 or more 
elements''.  So, in this example, you're splicing in a list of either 1 
element or 0 elements.  You could also splice in 2 or more elements, if 
you wanted.

You can play around with this (``quasiquote splicing'') in DrRacket, by 
evaluating examples like this:

`(a b ,@(list 'x 'y) c d) ;==> (a b x y c d)
`(a b ,@(list      ) c d) ;==> (a b c d)
`(a b ,@(if #t '(x y) '()) c d) ;==> (a b x y c d)

-- 
http://www.neilvandyke.org/


Posted on the users mailing list.