Re: any() and all() on empty list?



Steve R. Hastings wrote:

Therefore, I propose that all() should work as if it were written this way:

def all(S):
ret_val = False

for x in S:
ret_val = True
if not x:
return False

return ret_val

Comments?

Ant wrote:

all(list) simply means "every element of the list evaluates to True".
This is trivially true in the case of the empty list. This is logically
equivalent to "There are no elements in the list which evaluate to
False".

any(list) simply means "at least one element of the list evaluates to
true". This is trivially false for the empty list - there are no
elements to be true.

These are logical functions and should be mathematically sound. It's
possible to add all sorts of problems if we just arbitrarily decide
what "for all x" should mean. We may just as well decide that for
convenience: math.pi == 3.


I agree.

Some amateur maths - applying the identities of a 'two-element Boolean
algebra' found here:

http://en.wikipedia.org/wiki/Two-element_Boolean_algebra

def any(S):
for x in S:
if x:
return True
return False

def all(S):
for x in S:
if not x:
return False
return True

#the identities don't hold if you use the alternative
##def all(S):
## ret_val = False
##
## for x in S:
## ret_val = True
## if not x:
## return False
##
## return ret_val

empty = []
universe = [ 0, 1 ]

one = all(empty)
zero = any(empty)

assert (one or one) == one
assert (one or zero) == one
assert (zero or one) == one
assert (zero or zero) == zero
assert (zero and zero) == zero
assert (zero and one) == zero
assert (one and zero) == zero
assert (one and one) == one
assert (not one) == zero
assert (not zero) == one

#on the other hand
one = all(universe)
zero = any(universe)

#de Morgan - swap 'and' and 'or' and complement the result
assert not(one and one) != one
assert not(one and zero) != one
assert not(zero and one) != one
assert not(zero and zero) != zero
assert not(zero or zero) != zero
assert not(zero or one) != zero
assert not(one or zero) != zero
assert not(one or one) != one
assert not(not one) != zero
assert not(not zero) != one

----------------------------------------------------------------------

Gerard

.



Relevant Pages

  • Non-zero gaps between real numbers
    ... assert that there is no non-zero extent gap devoid of real numbers in ... numbers with zero gap between them. ... This persistence of non-zero extent is safe to stick to, ...
    (sci.math)
  • Re: INTEGER*3
    ... of conversion to determine how MINLOC and MAXLOC would work? ... to me to make more sense to assert that the type implements certain ... operator, operatorand ZERO, ... transformational intrinsics seem to be hard to implement, ...
    (comp.lang.fortran)
  • Re: Generic In-Place Sort
    ... > provided by ANSICompareStr or ANSICompareText. ... > values less than zero, zero, or greater than zero, but not necessarily ... > thought an ASSERT was a more appropriate approach here, ... > If you used an assert instead of an exception here you would not need ...
    (comp.lang.pascal.delphi.misc)
  • Re: Will the following code assert:
    ... IEEE standard floats have a defined bit pattern. ... There is also a -0 where the bit pattern is not all zero. ... Your assert will probably fail in these cases. ...
    (microsoft.public.vc.language)
  • Re: Macro unnecessary
    ... puts "very positive" ... puts "only zero" ... proc(print "myObj has the value 15")} ...
    (comp.lang.lisp)

Loading