help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "Backquote constructs" to "splice" values without "eval".


From: Barry Margolin
Subject: Re: "Backquote constructs" to "splice" values without "eval".
Date: Mon, 07 Jan 2013 15:51:26 -0500
User-agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)

In article <address@hidden>,
 Oleksandr Gavenko <address@hidden> wrote:

> On 2013-01-07, Barry Margolin wrote:
> 
> >> I construct TLV (table-len-val) structs in string.
> >>
> >> Is it possible to omit "eval" from second line by using some sugar code:
> >>
> >>   (setq binstr-len 4)
> >>   (setq binstr (eval `(unibyte-string ?s binstr-len ,@(make-list binstr-len
> >>   ?x))))
> >>   (assert (eq (+ 2 binstr-len) (length binstr)))
> >>
> >> Another solution:
> >>
> >>   (setq binstr (concat (unibyte-string ?s binstr-len) (make-list binstr-len
> >>   ?x)))
> >>
> >> Or "apply" stands for this purpose(??):
> >>
> >>   (setq binstr (apply 'unibyte-string ?s binstr-len (make-list binstr-len
> >>   ?x)))
> >
> > The "apply" solution is usually the correct way to do it.
> 
> I also start thinking about "apply" with several list inside it:
> 
>   (apply '+ 1 '(2) '(3 4))
> 
> But above expression fail (only last arg expanded as list of args). To resolve
> this issue I use expression:
> 
>   (apply '+ 1 (append '(2) '(3 4)))
> 
> But how about expression with atoms between (??):
> 
>   '(1) 2 '(3 4)
> 
> I write non-linear code:
> 
>   (apply '+ (append '(1) (cons 2 '(3 4))))
> 
> How to avoid call to "cons"?

Now you can go back to using backquote to construct the list:

(apply #'+ `(,@'(1) 2 ,@'(3 4)))

But you still don't need to use eval.

-- 
Barry Margolin, address@hidden
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


reply via email to

[Prev in Thread] Current Thread [Next in Thread]