From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Drew Adams Newsgroups: gmane.emacs.help Subject: RE: member returns list (was: Re: To `boundp' or not to `boundp'?) Date: Tue, 1 Sep 2015 23:21:15 -0700 (PDT) Message-ID: <2d84e377-3f73-48f0-b351-6ca5b94d6f47@default> References: <55E5C99B.3020608@yandex.ru> <87lhcpu2wb.fsf_-_@debian.uxu> <3c9412c3-3fff-446c-9e55-e8169b6a913d@default> <87zj15r7gs.fsf@debian.uxu> <75212b6a-133a-401a-8051-3931a7a40958@default> <87r3mhr55z.fsf@debian.uxu> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1441174916 12061 80.91.229.3 (2 Sep 2015 06:21:56 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 2 Sep 2015 06:21:56 +0000 (UTC) To: Emanuel Berg , help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Sep 02 08:21:43 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZX1QQ-0005Ja-L0 for geh-help-gnu-emacs@m.gmane.org; Wed, 02 Sep 2015 08:21:38 +0200 Original-Received: from localhost ([::1]:34186 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZX1QQ-0000pD-JV for geh-help-gnu-emacs@m.gmane.org; Wed, 02 Sep 2015 02:21:38 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48129) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZX1QC-0000oz-QS for help-gnu-emacs@gnu.org; Wed, 02 Sep 2015 02:21:25 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZX1Q7-0005qD-Tj for help-gnu-emacs@gnu.org; Wed, 02 Sep 2015 02:21:24 -0400 Original-Received: from aserp1040.oracle.com ([141.146.126.69]:28350) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZX1Q7-0005q9-NC for help-gnu-emacs@gnu.org; Wed, 02 Sep 2015 02:21:19 -0400 Original-Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id t826LH9S015913 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 2 Sep 2015 06:21:18 GMT Original-Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserv0021.oracle.com (8.13.8/8.13.8) with ESMTP id t826LG06005241 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Wed, 2 Sep 2015 06:21:16 GMT Original-Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by aserv0122.oracle.com (8.13.8/8.13.8) with ESMTP id t826LGic032023; Wed, 2 Sep 2015 06:21:16 GMT In-Reply-To: <87r3mhr55z.fsf@debian.uxu> X-Priority: 3 X-Mailer: Oracle Beehive Extensions for Outlook 2.0.1.9 (901082) [OL 12.0.6691.5000 (x86)] X-Source-IP: aserv0021.oracle.com [141.146.126.233] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 141.146.126.69 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:106979 Archived-At: > It looks like this: (member ELT LIST) >=20 > When is it beneficial to, instead of just getting > a `t' on a hit, to get a list that consists of ELT and > everything eastward of ELT, in LIST? When that is what you want/need. ;-) > The `car' example I provided may look cool (?) but it > isn't anything that cannot be done with "memberp", > because obviously the car is known, otherwise one > wouldn't be able to search for it. Sure, if you just want the element you're testing for, then (car (member 'foo xs)) is no better than (and (memberp 'foo xs) 'foo) (In the second example, if the element needs to be computed, let-bind it to avoid computing it twice.) But maybe you want the next element after `foo': (cadr (member 'foo xs)) Or the element after `foo' or the first element if `foo' is not in the list: (or (cadr (member 'foo xs)) (car xs)) Or maybe you want to change `foo' to `bar' in the list: (setcar (member 'foo xs) 'bar) Or maybe you want to truncate the list after `foo': (setcdr (member 'foo xs) ()) Or maybe you want to ensure that 'foo is at the head of the list, adding it there if not already in the list: (if (null xs) (setq xs (list 'foo)) (let ((tl (member 'foo xs))) (unless (eq tl xs) (when tl (setcdr (nthcdr (1- (- (length xs) (length tl)))=20 xs) (cdr tl))) (setq xs (cons 'foo xs)))) xs) Or the same thing for any list variable and element: (defun put-at-head (list-var element) (let* ((lis (symbol-value list-var)) (tl (member element lis))) (cond ((null lis) (set list-var (list element))) ((not (eq tl lis)) (when tl (setcdr (nthcdr (1- (- (length lis) (length tl))) lis)=20 (cdr tl))) (set list-var (cons element lis))))) (symbol-value list-var)) You get the idea. Often, if you care about the list structure it is because you are modifying it (destructive operations). (Not always - e.g., (cadr (member 'foo xs)).) =20 And yes, most other uses of `member' (and `memq') are just tests for membership. You can see this by grepping the Emacs Lisp sources.