From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: member returns list
Date: Thu, 03 Sep 2015 07:17:41 +0200 [thread overview]
Message-ID: <878u8occ96.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.370.1441156160.19560.help-gnu-emacs@gnu.org
Emanuel Berg <embe8573@student.uu.se> writes:
> Drew Adams <drew.adams@oracle.com> 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
next prev parent reply other threads:[~2015-09-03 5:17 UTC|newest]
Thread overview: 103+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-01 15:50 To `boundp' or not to `boundp'? Alexander Shukaev
2015-09-01 15:51 ` Dmitry Gutov
2015-09-01 15:53 ` Alexander Shukaev
2015-09-01 16:42 ` Drew Adams
2015-09-01 17:13 ` Michael Heerdegen
2015-09-01 18:00 ` Drew Adams
[not found] ` <mailman.331.1441125765.19560.help-gnu-emacs@gnu.org>
2015-09-01 20:20 ` Barry Margolin
2015-09-01 23:37 ` member returns list (was: Re: To `boundp' or not to `boundp'?) Emanuel Berg
2015-09-02 0:10 ` Drew Adams
2015-09-02 0:27 ` Emanuel Berg
2015-09-02 0:38 ` Drew Adams
2015-09-02 1:17 ` Emanuel Berg
2015-09-02 6:21 ` Drew Adams
2015-09-02 21:20 ` Emanuel Berg
[not found] ` <mailman.370.1441156160.19560.help-gnu-emacs@gnu.org>
2015-09-03 5:17 ` Pascal J. Bourguignon [this message]
2015-09-04 0:26 ` member returns list Emanuel Berg
2015-09-02 16:45 ` Michael Heerdegen
2015-09-02 21:47 ` Emanuel Berg
2015-09-02 22:09 ` Michael Heerdegen
2015-09-02 22:11 ` Drew Adams
2015-09-02 22:13 ` Marcin Borkowski
2015-09-04 1:03 ` Emanuel Berg
2015-09-04 6:50 ` Marcin Borkowski
2015-09-05 17:35 ` Emanuel Berg
2015-09-05 17:55 ` Random832
2015-09-05 18:09 ` Emanuel Berg
2015-09-05 21:04 ` Random832
2015-09-06 2:57 ` Emanuel Berg
[not found] ` <mailman.552.1441475739.19560.help-gnu-emacs@gnu.org>
2015-09-05 23:39 ` Pascal J. Bourguignon
2015-09-06 2:46 ` Emanuel Berg
2015-09-09 1:48 ` Robert Thorpe
2015-09-09 2:44 ` Pascal J. Bourguignon
2015-09-09 3:19 ` John Mastro
2015-09-10 0:25 ` Emanuel Berg
[not found] ` <mailman.557.1441507097.19560.help-gnu-emacs@gnu.org>
2015-09-06 21:20 ` Pascal J. Bourguignon
2015-09-06 23:33 ` Emanuel Berg
[not found] ` <mailman.598.1441581917.19560.help-gnu-emacs@gnu.org>
2015-09-06 23:47 ` Pascal J. Bourguignon
2015-09-07 1:29 ` Pascal J. Bourguignon
2015-09-07 2:25 ` Emanuel Berg
2015-09-09 1:44 ` Robert Thorpe
2015-09-10 0:23 ` Emanuel Berg
[not found] ` <mailman.793.1441844665.19560.help-gnu-emacs@gnu.org>
2015-09-10 2:01 ` Pascal J. Bourguignon
2015-09-10 2:32 ` Emanuel Berg
[not found] ` <ECCA0E56-A051-4D38-8CBB-6CE9208BB047@openmailbox.org>
2015-09-11 1:32 ` Emanuel Berg
2015-09-11 1:46 ` Emanuel Berg
[not found] ` <mailman.797.1441852386.19560.help-gnu-emacs@gnu.org>
2015-09-10 4:21 ` Pascal J. Bourguignon
2015-09-10 18:09 ` Alex Kost
2015-09-11 1:01 ` Random832
2015-09-11 1:28 ` Emanuel Berg
2015-09-11 2:01 ` Emanuel Berg
2015-09-11 2:52 ` Random832
[not found] ` <mailman.890.1441936900.19560.help-gnu-emacs@gnu.org>
2015-09-11 2:27 ` Pascal J. Bourguignon
2015-09-12 0:10 ` Emanuel Berg
2015-09-11 2:37 ` Ian Zimmerman
2015-09-11 1:03 ` Emanuel Berg
2015-09-11 0:57 ` Emanuel Berg
2015-09-11 5:42 ` tomas
2015-09-12 0:18 ` Emanuel Berg
2015-09-13 8:12 ` tomas
[not found] ` <mailman.897.1441950163.19560.help-gnu-emacs@gnu.org>
2015-09-11 23:29 ` Pascal J. Bourguignon
2015-09-12 0:20 ` Emanuel Berg
[not found] ` <mailman.966.1442016910.19560.help-gnu-emacs@gnu.org>
2015-09-12 2:01 ` Pascal J. Bourguignon
2015-09-12 3:26 ` Emanuel Berg
2015-09-13 7:50 ` tomas
2015-09-12 2:03 ` Pascal J. Bourguignon
2015-09-12 2:45 ` Stefan Monnier
2015-09-12 3:12 ` Emanuel Berg
2015-09-12 3:31 ` Emanuel Berg
[not found] ` <mailman.972.1442025961.19560.help-gnu-emacs@gnu.org>
2015-09-12 6:16 ` Pascal J. Bourguignon
2015-09-12 23:38 ` Emanuel Berg
2015-09-13 8:03 ` tomas
2015-09-14 1:31 ` Emanuel Berg
[not found] ` <mailman.1016.1442100597.19560.help-gnu-emacs@gnu.org>
2015-09-13 0:09 ` Pascal J. Bourguignon
2015-09-13 0:24 ` John Mastro
[not found] ` <mailman.1019.1442103883.19560.help-gnu-emacs@gnu.org>
2015-09-13 4:29 ` Pascal J. Bourguignon
2015-09-16 0:01 ` computer history (was: Re: member returns list) Emanuel Berg
[not found] ` <mailman.978.1442028310.19560.help-gnu-emacs@gnu.org>
2015-09-12 6:18 ` member returns list Pascal J. Bourguignon
2015-09-12 23:56 ` Emanuel Berg
[not found] ` <mailman.1018.1442101697.19560.help-gnu-emacs@gnu.org>
2015-09-13 0:20 ` Pascal J. Bourguignon
2015-09-14 1:24 ` Emanuel Berg
2015-09-12 2:04 ` Pascal J. Bourguignon
2015-09-12 3:15 ` Emanuel Berg
2015-09-12 7:40 ` Eli Zaretskii
[not found] ` <mailman.884.1441933051.19560.help-gnu-emacs@gnu.org>
2015-09-11 2:26 ` Pascal J. Bourguignon
2015-09-12 0:16 ` Emanuel Berg
2015-09-07 2:38 ` Drew Adams
2015-09-07 2:37 ` Drew Adams
[not found] ` <mailman.502.1441349470.19560.help-gnu-emacs@gnu.org>
2015-09-04 17:26 ` Barry Margolin
2015-09-05 17:40 ` Emanuel Berg
2015-09-02 0:40 ` member (was: Re: To `boundp' or not to `boundp'?) Emanuel Berg
[not found] ` <mailman.363.1441150201.19560.help-gnu-emacs@gnu.org>
2015-09-02 16:25 ` member returns list " Barry Margolin
2015-09-02 21:35 ` Emanuel Berg
[not found] ` <mailman.426.1441229773.19560.help-gnu-emacs@gnu.org>
2015-09-02 22:56 ` Barry Margolin
2015-09-02 23:23 ` Emanuel Berg
[not found] ` <mailman.368.1441153951.19560.help-gnu-emacs@gnu.org>
2015-09-03 5:19 ` member Pascal J. Bourguignon
2015-09-04 0:24 ` member Emanuel Berg
[not found] ` <<barmar-B5D67F.16201001092015@88-209-239-213.giganet.hu>
2015-09-01 20:42 ` To `boundp' or not to `boundp'? Drew Adams
2015-09-02 2:12 ` Stefan Monnier
2015-09-02 15:26 ` Alexander Shukaev
2015-09-02 16:51 ` Michael Heerdegen
[not found] ` <mailman.410.1441212753.19560.help-gnu-emacs@gnu.org>
2015-09-02 20:00 ` Barry Margolin
[not found] <mailman.719.1441763081.19560.help-gnu-emacs@gnu.org>
2015-09-09 15:03 ` member returns list Barry Margolin
2015-09-09 19:44 ` Ted Zlatanov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=878u8occ96.fsf@kuiper.lan.informatimago.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.