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 10:02:35 -0500
User-agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)

In article <mailman.16855.1357563071.855.help-gnu-emacs@gnu.org>,
 Oleksandr Gavenko <gavenkoa@gmail.com> 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.

> 
> Seems that "`" use current variable values when sexp *parsed*, while with
> "apply" it use variable values on *evaluation*. Is I am right?

If you do:

(setq sexp `(unibyte-string ?s binstr-len ,@(make-list binstr-len ?x))
(setq binstr-len 6)
(eval sexp)

The first BINSTR-LEN in SEXP will be 6 because it's evaluated by the 
call to EVAL, the second one will be 4 because it's evaluated during the 
SETQ.  In neither case is it evaluated at read time; remember, backquote 
is just a shorthand for code that constructs the list at the time the 
expression is evaluated, so the first line is equivalent to:

(setq sexp (list* 'unibyte-string '?s 'binstr-len (make-list binstr-len 
?x)))

As you can see, the first UNIBYTE-STRING is quoted, the second one is 
not (because of the comma in the backquote expression), so the latter 
gets evaluated at the time of the SETQ.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


reply via email to

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