all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Kodi Arfer <withheld@domain.tld>
To: help-gnu-emacs@gnu.org
Subject: Combining lists in Emacs Lisp
Date: Thu, 06 Dec 2007 20:39:40 +0000	[thread overview]
Message-ID: <a45e9$47585e0c$8dc3973d$30657@ALLTEL.NET> (raw)

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?

             reply	other threads:[~2007-12-06 20:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-06 20:39 Kodi Arfer [this message]
2007-12-06 22:38 ` Combining lists in Emacs Lisp David Kastrup
2007-12-10 12:08   ` Kodi Arfer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='a45e9$47585e0c$8dc3973d$30657@ALLTEL.NET' \
    --to=withheld@domain.tld \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.