[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Extract sublists
From: |
Ted Zlatanov |
Subject: |
Re: Extract sublists |
Date: |
Tue, 17 Nov 2009 09:37:50 -0600 |
User-agent: |
Gnus/5.110011 (No Gnus v0.11) Emacs/23.1.50 (gnu/linux) |
On Tue, 17 Nov 2009 05:22:51 -0800 (PST) Nordlöw <per.nordlow@gmail.com> wrote:
N> Is there a function for extracting sublists of lists?
N> If not here is my suggestion for inclusion in Emacs.
N> (defun sublist (list from to)
N> "Return a sublist of LIST, from FROM to TO.
N> Counting starts at 0. Like `substring' but for lists."
N> (let (rtn (c from))
N> (setq list (nthcdr from list))
N> (while (and list (< c to))
N> (push (pop list) rtn)
N> (setq c (1+ c)))
N> (nreverse rtn)))
N> ;; Use: (sublist '(a b) 0 0)
N> ;; Use: (sublist '(a b) 0 1)
N> ;; Use: (sublist '(a b) 1 2)
N> ;; Use: (sublist '(a b) 0 2)
It would be really nice if either FROM or TO could be a function or a
number, so you can say "from 2 to the first place where the element
passed to this function doesn't return t." This is not a filter because
you look for the first place the function fails. Unfortunately it's
efficient only if you walk through the list yourself so nthcdr won't be
so useful.
Ted