From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: member returns list Date: Thu, 03 Sep 2015 07:17:41 +0200 Organization: Informatimago Message-ID: <878u8occ96.fsf@kuiper.lan.informatimago.com> 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> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1441257640 3698 80.91.229.3 (3 Sep 2015 05:20:40 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 3 Sep 2015 05:20:40 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Sep 03 07:20:33 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 1ZXMwp-0001I9-8Q for geh-help-gnu-emacs@m.gmane.org; Thu, 03 Sep 2015 07:20:31 +0200 Original-Received: from localhost ([::1]:44716 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZXMwp-000730-Cb for geh-help-gnu-emacs@m.gmane.org; Thu, 03 Sep 2015 01:20:31 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 104 Original-X-Trace: individual.net aBMlikCIeOgIXgJhAWNa0ArMbKxUwRMyoionMsJX4ibSjEF3ue Cancel-Lock: sha1:MDU1Yzg1OTcyNThmYTk4MTMzNTliOWUxYWUwODNlMDJhNzZiNjUwZA== sha1:hQ/s14JaHaf/EhcQ0IsRlBnxLjc= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:214719 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:107004 Archived-At: Emanuel Berg writes: > Drew Adams writes: > >> I don't understand the question. It always functions >> the same way. Whether code uses it only to test >> membership or to access the first tail that >> corresponds is up to that code. But `member' behaves >> ("functions") the same. > > It looks like this: > > (member ELT LIST) > > 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? > > 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. But member* is like CL:MEMBER, and take :KEY and :TEST arguments that could make the matched element quite different from the target. (member* 1 '(2 4 6 5 7 9) :test (lambda (a b) (evenp (- a b)))) --> (5 7 9) (member* 3 '(1 4 9 16 25) :key (function sqrt) :test (function =)) --> (9 16 25) An other example: ;; define an order on colors: (defvar *order* '(red orange yellow green blue indigo violet)) (defun le (a b) (member b (member a *order*))) (le 'orange 'blue) --> (blue indigo violet) (le 'orange 'orange) --> (orange yellow green blue indigo violet) (le 'blue 'orange) --> nil Yet another example, not using car, but second: (defun rps-wins-over (a b) (eql b (second (member a '(paper rock scissors paper))))) (rps-wins-over 'scissors 'paper) --> t (rps-wins-over 'paper 'scissors) --> nil Another example: (let ((list '(1 2 3 4 5 6)) (terminator 4)) (ldiff list (member terminator list))) --> (1 2 3) Another example: in the algorith to process include CPP directories, I have a use of member value such as: (let ((include-directories (include-directories kind))) (when (eq directive :include-next) (setf include-directories (cdr (member (context-directory context) include-directories :test (function equal))))) (multiple-value-bind (path directory) (search-file-in-directories include-file include-directories kind directive) (cond ((eq t path) #|done|#) (path (include path directory)) (t (cpp-error …))))) Another example: since member returns the cons cell where the target object is stored, we can update it. For example, when implementing a cache, I used something like: (defun update-cache (name) (let ((sl (member* name *cache* :key 'object-name :test 'string-equal))) (if sl (setf (car sl) new-data) (push new-data *cache*)))) In CL, there's another predicate that returns a very useful value: digit-char-p is specified to return the integer value of the digit character: (digit-char-p #\4) --> 4 (digit-char-p #\!) --> nil In general, in CL, predicates are specified to return _generalized_ booleans, so implementation may return useful values for them too (but it would be non-conforming to take advantage of those "useful" values). -- __Pascal Bourguignon__ http://www.informatimago.com/ “The factory of the future will have only two employees, a man and a dog. The man will be there to feed the dog. The dog will be there to keep the man from touching the equipment.” -- Carl Bass CEO Autodesk