In article <··································@4ax.com>, TejimaNoHimitsu wrote:
> Is there a really easy way to check if an element exists in a list? I know
> I could do (member 'b '(a b c)) but what about a more complex list? For
> example...
>
> '(b a (t y (q w (z c)) v) d) and I wanted to check to see if the element v
> is in there. Is there a lisp function that does this? I don't think there
> is, but I thought it couldn't hurt to ask the experts :)
Warning: I don't consider myself as an expert. :)
In Paul Graham's _On Lisp_, available from paulgraham.com, in section 5.6,
"Recursion on Subtrees", you will find:
(defun rfind-if (fn tree)
(if (atom tree)
(and (funcall fn tree) tree)
(or (rfind-if fn (car tree))
(if (cdr tree)
(rfind-if fn (cdr tree))))))
Called something like so:
(rfind-if <predicate> <tree>)
Example:
(let ((find-me 'v)
(look-in '(b a (t y (q w (z c)) v) d)))
(rfind-if #'(lambda (element) (eql find-me element))
look-in))
=> V
Also, just because I feel pedantic, you said:
> Is there a lisp function that does this? I don't think there is, [...]
As you can see, such a function does exist. Did you mean "does Common Lisp
pre-define such a function?"? It may (see initial disclaimer), but I don't
think so. What do you mean by "is there a function"? :)
Probably a Lisp function exists that could read your post, understand it
(whatever that means), and answer it ... writing such a function, ah, there's
the rub! :)
-- Larry