[Top][All Lists]

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

Combining lists in Emacs Lisp

From: Kodi Arfer
Subject: Combining lists in Emacs Lisp
Date: Thu, 06 Dec 2007 20:39:40 +0000
User-agent: Pan/0.129 (Benson & Hedges Moscow Gold)

Over the past three days or so, I've been giving myself a crash course in 
Emacs Lisp (so I can do crazy things in my .emacs) by reading the 
reference manual and trying out stuff in Lisp Interaction mode. I think 
I'm finally getting the basics down; the one thing I'm having trouble 
with right now is this: I can't figure out how best to interpolate lists 
into function calls. Let me give an example of what I mean. In Perl, my 
favorite computer language, list interpolation happens automagically. So, 
suppose I want the sum of 4, 3, and the elements of the lists stored in 
the variables @foo and @bar. If I've defined some function "sum" that 
works like Lisp's "+", I can just say

sum(4, @foo, 3, @bar)

and supposing @foo contains (1, 2) and @bar contains (0, 0), Perl will 
turn that expression into

sum(4, 1, 2, 3, 0, 0)

before actually calling the sum function. So for the related situation in 
Emacs Lisp, I'm inclined to try

(+ 4 foo 3 bar)

But after the arguments are evaluated, the expression becomes

(+ 4 (1 2) 3 (0 0))

which, of course, isn't what I meant at all. Now, I know that I could, 
for instance, define a new function that works like + but also operates 
on lists by recursively applying + to their elements. But just adding 
things isn't the point here; I want a general way to splice lists 
together seamlessly. I know there are various ways I can work around this 
problem with eval, such as

(eval `(+ 4 ,@foo 3 ,@bar))

but that seems inelegant. I should think there'd be some way to 
interpolate lists using only the implicit evaluation that each argument 
of a function call gets. Isn't building up the arguments of a function 
call from lists a fairly common task? Even dotted-pair notation can't 
seem to overcome this difficulty, since the right-hand operand of a dot 
isn't evaluated.

Is there any good way to do what I'm trying to do here? Or am I simply 
trying to program in an un-Lispish style? If the latter is the case, 
what's the typical way a Lisp programmer would accomplish my addition 

reply via email to

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