[racket] #<void> in xexpr

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Sat Dec 10 01:22:51 EST 2011

, takes an expression and puts it in directly

`(something ,(list 1 2 3) else) ===> '(something (1 2 3) else)

,@ takes an expression that evaluates to a list and "splices" it in place

`(something ,@(list 1 2 3) else) ===> '(something 1 2 3 else)

I typically do something like

`(something ,@(if condition (list answer) empty) else)

and even may make a macro out of it

(define-syntax-rule (when-xexpr cond expr)
 (if cond (list expr) empty))

Jay

On Fri, Dec 9, 2011 at 11:12 PM, Jordan Schatz <jordan at noionlabs.com> wrote:

> I think thats there must be a "lisp" way of doing this, but I'm still too
> new to see how. Anyone care to enlighten me?
>
> I'm trying to create a function that produces an xexpr:
>
> (define (custom-input name
>                      #:class [class "xlarge"]
>                      #:id [id #f]
>                      #:size [size 30]
>                      #:type [type "text"]
>                      #:placeholder [placeholder #f]
>                      #:autofocus [autofocus #f]
>                      #:label [label #f])
>  `(div ((class "clearfix"))
>        ,(if label
>           `(label ((for ,name)) ,name)
>           '())
>        (div ((class "input"))
>             (input ((name ,name) (class ,class)
>                     (size ,(number->string size)) (type ,type)
>                     ,(if id
>                          `(id ,id)
>                          `(id ,name))
>                     ,(when placeholder
>                        `(placeholder ,placeholder))
>                     ,(when autofocus
>                          '(autofocus "autofocus")))))))
>
> But using "when" produces #<void> like so:
>
> (validate-xexpr
>  (custom-input "Company"
>               #:label #t
>               #:placeholder "Your company name (optional)"))
>
>  > Expected a symbol as the element name, given (name "Company")
>
> Substituting an
> (if condition
>    then-branch
>    empty)
> for the "when" also produces an invalid xexpr, though I'd rather not use
> if and have an else branch unless I really wanted it to be there.
>
> Thanks,
> Jordan
>
>
>
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20111209/b69cc0f5/attachment.html>

Posted on the users mailing list.