From: David Bakhash
Subject: what's the fundamental problem w/ `and'?
Date:
Message-ID: <cxjbtmd33c2.fsf@engc.bu.edu>
Since you can't, apply #'and, what's the optimal way to make sure that
everything in a list is non-nil?
i.e. I'd love to do this:
(apply #'and my-list)
but, of course, that's not possible. I can do this:
(loop for element in my-list always element)
Is the above (loop) example a decent way to do this?
dave
In article <···············@engc.bu.edu>, David Bakhash <·····@bu.edu> wrote:
>Since you can't, apply #'and, what's the optimal way to make sure that
>everything in a list is non-nil?
Not sure about "optimal", but.....
(every #'identity my-list )
http://www.harlequin.com/education/books/HyperSpec/Body/fun_everycm_s_erycm_notany.html#every
EVERY and SOME (and the like ) are "substitutes" for apply of "and"
and "or".
--
Lyman S. Taylor "Twinkie Cream; food of the Gods"
(·····@cc.gatech.edu) Jarod, "The Pretender"
In article <··········@pravda.cc.gatech.edu>,
·····@cc.gatech.edu (Lyman S. Taylor) wrote:
> In article <···············@engc.bu.edu>, David Bakhash <·····@bu.edu> wrote:
> >Since you can't, apply #'and, what's the optimal way to make sure that
> >everything in a list is non-nil?
>
> Not sure about "optimal", but.....
>
> (every #'identity my-list )
>
>
http://www.harlequin.com/education/books/HyperSpec/Body/fun_everycm_s_erycm_nota
ny.html#every
>
> EVERY and SOME (and the like ) are "substitutes" for apply of "and"
> and "or".
With the added bonus of not being constrained by CALL-ARGUMENTS-LIMIT. Using
APPLY to implement ANY would work only on lists of length not exceeding that
limit (which can be as low as 50).
--Vassili
--
Vassili Bykov
The Object People
http://www.objectpeople.com
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
David Bakhash <·····@bu.edu> writes:
> Since you can't, apply #'and, what's the optimal way to make sure that
> everything in a list is non-nil?
>
> i.e. I'd love to do this:
>
> (apply #'and my-list)
>
> but, of course, that's not possible. I can do this:
Apart from the fact that and is not applyable, you wouldn't want to do
this anyways:
- The length of my-list might easily exceed call-arguments-limit,
which might be as small as 50[1].
- It isn't normally considered good style anyways to use &rest
parameters to pass large amounts of data around. Use &rest to pass
a variable, but bounded, number of parameters around. Pass lists as
single arguments, unless the list data is really a parameter list.
- applying functions on large lists might also not be very efficient,
since apply might or might-not copy the lists before calling the
function.
> (loop for element in my-list always element)
>
> Is the above (loop) example a decent way to do this?
IMHO it's a decent, if verbose way to do this.
(every #'identity my-list)
is another, shorter way of doing this, although -- depending on the
implementation -- it might be somewhat slower. OTOH it will work on
sequences and isn't restricted to lists.
But IMNSHO it doesn't matter, since you will do
(defun all-true (seq)
"Ensures that all elements of sequence are non-nil."
;; Your favourite implementation here, possibly with implementation-
;; dependant optimizations. In CMU sequence-functions are very bad
;; on lists, since it uses the equivalent of elt/nth
#-CMU
(every #'identity seq)
#+CMU
(if (listp seq)
(loop for elem in seq always elem)
(every #'identity seq)))
anyways to provide a more idiomatic (and shorter) way of doing this
(hint,hint ;). IIRC Paul Graham did dwell on this particular example
in either "ANSI Common Lisp" or "On Lisp"...
Regs, Pierre.
Footnotes:
[1] Here is the excerpt from the HyperSpec (see
http://www.harlequin.com/books/HyperSpec/Body/convar_call-a_uments-limit.html):
Constant Variable CALL-ARGUMENTS-LIMIT
Constant Value:
An integer not smaller than 50 and at least as great as the value of
lambda-parameters-limit, the exact magnitude of which is
implementation-dependent.
Description:
The upper exclusive bound on the number of arguments that may be
passed to a function.
Examples: None.
See Also:
lambda-parameters-limit, multiple-values-limit
Notes: None.
--
Pierre Mai <····@acm.org> http://home.pages.de/~trillian/
"One smaller motivation which, in part, stems from altruism is Microsoft-
bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]