MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: "short circuiting" And and Or

  • To: mathgroup at smc.vnet.net
  • Subject: [mg70607] Re: [mg70582] "short circuiting" And and Or
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Sat, 21 Oct 2006 05:13:47 -0400 (EDT)
  • References: <200610200921.FAA11062@smc.vnet.net>

On Oct 20, 2006, at 5:21 AM, Szabolcs Horvat wrote:

> I'd like to write a function that tests whether there are any elements
> in a list that violate a certain condition. I could simply use
> And@@condition/@list, but for reasons of efficiency I'd like to stop
> the testing as soon as a "False" value is found. Is there any elegant
> way of doing this without writing an explicit While loop?

Elegance is in the eye of the beholder but

NestWhile[Block[{l =
    Rest[#]}, If[l == {}, l, Prepend[Rest[l],
        And[First[#], condition[First[
         l]]]]]] &, Prepend[list, True], # != {} && First[#] &]

gives the desired result.  It returns a list containing the first  
element which for which condition is false followed by the rest of  
the original list.

Regards,

Ssezi


  • Prev by Date: Re: "short circuiting" And and Or
  • Next by Date: Plot3D question
  • Previous by thread: Re: "short circuiting" And and Or
  • Next by thread: Re: "short circuiting" And and Or