If (member o l)
finds o in the list l, it also returns the cdr of l beginning with o. This return value can be used, for example, to test for duplication. If o is duplicated in l, then it will also be found in the cdr of the list returned by member
. This idiom is embodied in the next utility, duplicate
:
>(duplicate ’a ’(a b c a d))
(A D)
(defun duplicate (obj lst &key (test #’eql)) (member obj (cdr (member obj lst :test test)) :test test))
— p.51
— On Lisp
— Paul Graham
.
Exercise 4.4
Without using the existing function member
, define duplicate
as in
>(duplicate ’a ’(a b c a d))
(A D)
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
— This answer is my guess. —
(defun my-member (obj lst) (cond ((not lst) NIL) ((eq obj (car lst)) lst) (t (my-member obj (cdr lst)))))
— This answer is my guess. —
— Me@2019-01-21 06:34:46 AM
.
.
2019.01.21 Monday (c) All rights reserved by ACHK