From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ted Zlatanov Newsgroups: gmane.emacs.help Subject: Re: Extract sublists Date: Tue, 17 Nov 2009 09:37:50 -0600 Organization: =?utf-8?B?0KLQtdC+0LTQvtGAINCX0LvQsNGC0LDQvdC+0LI=?= @ Cienfuegos Message-ID: <87skcdf9gx.fsf@lifelogs.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1258472522 24658 80.91.229.12 (17 Nov 2009 15:42:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 17 Nov 2009 15:42:02 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Nov 17 16:41:56 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NAQBj-0003Up-La for geh-help-gnu-emacs@m.gmane.org; Tue, 17 Nov 2009 16:41:51 +0100 Original-Received: from localhost ([127.0.0.1]:52301 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NAQBj-0001XB-5y for geh-help-gnu-emacs@m.gmane.org; Tue, 17 Nov 2009 10:41:51 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!postnews.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!news.albasani.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 27 Original-X-Trace: news.albasani.net xBKI+gWY1c1OZLvcx0Ecf+kPCn4bDV5S7KzI4mCWPnNaW9KaAx9frCjJCLqBvmrrmnFhg2wpsWW1if2/PQvOrkhlWsCeCnL8pnWAa53LTpddVSwhWuqEt2FSaKuDVmLZ Original-X-Complaints-To: abuse@albasani.net Original-NNTP-Posting-Date: Tue, 17 Nov 2009 15:37:51 +0000 (UTC) X-User-ID: s8r/lMA1tQPLglsSB2q9orxWjp9tXmNXdA9Wjvv0afI= X-Face: bd.DQ~'29fIs`T_%O%C\g%6jW)yi[zuz6; d4V0`@y-~$#3P_Ng{@m+e4o<4P'#(_GJQ%TT= D}[Ep*b!\e,fBZ'j_+#"Ps?s2!4H2-Y"sx" Cancel-Lock: sha1:GsFSVfyWH2V4kVcIaL0DUaA/lpg= sha1:V9hB29CX1ZVZDtgIxl/WhPP0rPA= User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.1.50 (gnu/linux) X-NNTP-Posting-Host: 2tItqGlTmtwKRG1txoEdzby4/Qyu/2D2tWFqcZD9Vhc= Original-Xref: news.stanford.edu gnu.emacs.help:174760 X-Original-Bytes: 2316 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:69833 Archived-At: On Tue, 17 Nov 2009 05:22:51 -0800 (PST) Nordlöw 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