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: Jambunathan K
Subject: Re: "Backquote constructs" to "splice" values without "eval".
Date: Tue, 08 Jan 2013 02:40:29 +0530
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Oleksandr Gavenko <gavenkoa@gmail.com> writes:

> 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"?

(apply '+ (loop for x in '(1 (2) (3 4))
                collect (if (numberp x) x (apply '+ x))))

There are also other predicates that you can use.  For eg., atom, consp,
listp.
-- 



reply via email to

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