Re: Effeciency of any predicate




On 27 Feb 2007 12:23:48 -0800, "S. Robert James" <srobertjames@xxxxxxxxx> said:

| Is there a more efficient way of doing this:
| (defun any? (predicate lst)
| (if (null lst)
| nil
| (or (funcall predicate (car lst))
| (any? predicate (cdr lst)))))

By the way, I'd suggest to write the above using COND, rather than nest
OR within an IF, for better clarity and style (and note that ENDP is
better than NULL in Common Lisp when testing for the end of a list):

(defun anyp (predicate list)
(cond ((endp list) nil)
((funcall predicate (first list)))
(t (anyp predicate (rest list)))))

(untested).

---Vassil.


--
mind mate, n.
One of two persons mentally compatible with each other (cf. soul mate).
.



Relevant Pages

  • Re: Paul Grahams rfind-if
    ... How long have you been using Lisp? ... Any function can serve as a predicate by returning either nil for false or any other value for true. ...
    (comp.lang.lisp)
  • Re: Effeciency of any predicate
    ... (predicate lst) ... (or (funcall predicate (car lst)) ... (defun any (predicate list) ... (or (funcall predicate (first list)) ...
    (comp.lang.lisp)