1

is there a function like find in lisp that returns true instead of the element we're trying to find?

example:

I want it to do

(find 'x '(a c x)) = t

not

(find 'x '(a c x)) = x

Also, the reason I am asking is because I am trying to reach the deepest element in a list. My plan was to flatten the list every time i recursively called it.

I would then stop the recursive call when

(mapcar 'atom list)

would tell me every atom in there is true.

Do you believe this is a good approach to this problem?

1
  • 1
    Why? In lisp anything but nil is true. However, a double negation will do what you want: i.e. (not (null (find ...)))
    – Terje D.
    Nov 1, 2012 at 8:01

1 Answer 1

1

There's no such function, but it can't be easier to write one:

(defun find-t (&rest args)
  (when (apply #'find args)
    t))

Also instead of (mapcar 'atom list) you can use (every #`(eql t %) list), i.e. check that every item in list is exactly t. (Here #`() is syntactic sugar for one-argument lambdas, that I use.)

But overall it's unclear, what you're trying to achieve with all this. Can you elaborate on what you're trying to do?

1
  • It's just as I said in the description. I'm trying to find the deepest atom in a list. So what i'm doing is flattening the list level by level. ex: i want (flatten '(a (b (d (g f))) e)) = (a b (d (g f )) e ) thus I'll be using find to check if there is any lists left inside the list. When there isn't i'll return the element that is the deepest in. However, right now I'm having trouble finding out how to flatten it level by level =P Nov 1, 2012 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.