* Combining lists in Emacs Lisp
@ 2007-12-06 20:39 Kodi Arfer
2007-12-06 22:38 ` David Kastrup
0 siblings, 1 reply; 3+ messages in thread
From: Kodi Arfer @ 2007-12-06 20:39 UTC (permalink / raw)
To: help-gnu-emacs
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
example?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Combining lists in Emacs Lisp
2007-12-06 20:39 Combining lists in Emacs Lisp Kodi Arfer
@ 2007-12-06 22:38 ` David Kastrup
2007-12-10 12:08 ` Kodi Arfer
0 siblings, 1 reply; 3+ messages in thread
From: David Kastrup @ 2007-12-06 22:38 UTC (permalink / raw)
To: help-gnu-emacs
Kodi Arfer <withheld@domain.tld> writes:
> 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))
You can try
(apply '+ 4 3 (append foo bar))
Which is not necessarily terribly efficient but should work.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Combining lists in Emacs Lisp
2007-12-06 22:38 ` David Kastrup
@ 2007-12-10 12:08 ` Kodi Arfer
0 siblings, 0 replies; 3+ messages in thread
From: Kodi Arfer @ 2007-12-10 12:08 UTC (permalink / raw)
To: help-gnu-emacs
On Thu, 06 Dec 2007 23:38:41 +0100, David Kastrup wrote:
> You can try
>
> (apply '+ 4 3 (append foo bar))
>
> Which is not necessarily terribly efficient but should work.
And it does work! Thank you. Even if the copying is inefficient (I assume
that's the inefficient part), I don't imagine there's any way to carve up
lists like this without copying something.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-12-10 12:08 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-12-06 20:39 Combining lists in Emacs Lisp Kodi Arfer
2007-12-06 22:38 ` David Kastrup
2007-12-10 12:08 ` Kodi Arfer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).