help-gnu-emacs
[Top][All Lists]
Advanced

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

Manipulating lists somewhere in the middle (index-based)


From: florian
Subject: Manipulating lists somewhere in the middle (index-based)
Date: Fri, 12 Jun 2009 13:16:46 -0700 (PDT)
User-agent: G2/1.0

I know there was a similar discussion yesterday (see
http://groups.google.de/group/gnu.emacs.help/browse_thread/thread/2fc2d1ea2229f3bc),
but that involved examining elements.

I am actually just wondering whether I reinvented the wheel in writing
two (non-destructive) functions:

(drop-nth 2 '(1 2 3 4 5))
=> (1 2 4 5)

(squeeze-in-at 2 "three" '(1 2 4 5))
=> (1 2 "three" 4 5)

My function definitions are probably not particularly interesting (if
anybody thinks they are, just let me know - they both simply work by
building a new list and returning it), but what I am wondering about
is this: when looking at the linked-paired-boxes notation the Elisp
manual uses to explain lists:

  --- ---      --- ---      --- ---
 |   |   |--> |   |   |--> |   |   |--> nil
  --- ---      --- ---      --- ---
   |            |            |
   |            |            |
    --> fir       --> oak      --> maple

[slightly modified]

it is actually very tempting to suspect there must be a way to
directly make the pointer from the cdr of "fir" point to the car of
the "maple" element and making the car of "oak" point to nil instead,
thus dropping the element from the list and getting (fir maple) as the
result.

Or, the other way round, it looks as if there could be a way to make
the pointer of the "oak" element point to a new cons cell, which has
"birch" as its car, and whose cdr we have point to the car of the
"maple" element, so as to get the list (fir oak birch maple).

Is there in fact some way to mess with these pointers, or is that a
purely didactic concept? Thanks for any input, it is really just
curiosity about Elisp!

Florian


reply via email to

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