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)))))
Does common Lisp have a built in which can reach the same goal?
"S. Robert James" <············@gmail.com> writes:
> 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)))))
>
> Does common Lisp have a built in which can reach the same goal?
SOME. You could implement ANY? as:
(defun any? (predicate lst)
(some predicate lst))
There is a nice set of similar functions documented here:
http://www.lispworks.com/documentation/HyperSpec/Body/f_everyc.htm
LST is not a very good variable name. Use LIST instead.
? as a suffix for a predicate is not very common in Common Lisp code.
Zach
On 27 Feb 2007 12:23:48 -0800, "S. Robert James" <············@gmail.com> 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).
S. Robert James wrote:
> 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)))))
>
> Does common Lisp have a built in which can reach the same goal?
Yes, it's called 'some.
There is also an equivalent in the loop macro, the loop keyword
'thereis. For example:
(loop for x in list
thereis (predicate x))
This can be useful for doing additional processing on the list elements
before performing the test, or for using predicates that have more than
one arguments and one argument should stay fixed. For example:
(loop for x in list thereis (eql x 24))
Style comments:
- In Common Lisp, it's unusual to use the question mark to indicate
predicates. It's more usual to have predicates end in p (like numberp,
listp, etc.). And even that is not a strict rule.
- It's also unusual to use 'lst for naming a variables. We typically say
'list.
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
Pascal Costanza <··@p-cos.net> writes:
> Style comments:
>
> - In Common Lisp, it's unusual to use the question mark to indicate
> predicates. It's more usual to have predicates end in p (like numberp,
> listp, etc.). And even that is not a strict rule.
A little bit of background that may help the original poster
understand this. On page 33 of CLtL2 Steele explains
Many people have suggested that brackets be used to
notate vectors, as [a b c] instead of #(a b c)
... However, to preserve the usefulness of the
user-definable macro-character feature of the function
read, it is necessary to leave some characters to the
user for this purpose.
http://www.franz.com/support/documentation/6.2/ansicl/subsecti/characte.htm
tells you that six characters are ! ? [ ] { }
The idea is that you are going to define a read macro for ?
and then you will have to escape any? as |any?| or any\? to
avoid triggering that read macro.
Alan Crowe
Edinburgh
Scotland
S. Robert James schrieb:
> 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)))))
>
> Does common Lisp have a built in which can reach the same goal?
Others toly you about SOME, but you could also say:
(defun any (predicate list)
(when list
(or (funcall predicate (first list))
(any predicate (rest list)))))
Andr�
--