Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

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

Search the Archive

RE: Re: build-in commutativity

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36731] RE: [mg36721] Re: build-in commutativity
  • From: "DrBob" <drbob at bigfoot.com>
  • Date: Sat, 21 Sep 2002 02:21:56 -0400 (EDT)
  • Reply-to: <drbob at bigfoot.com>
  • Sender: owner-wri-mathgroup at wolfram.com

Although And and Or don't have to evaluate all their arguments, I find
that this is useless in many situations I run into.  For instance, I
might have a long list 'boolList' of Boolean expressions and I want to
know if ANY of them are False, so I calculate

And@@boolList

In this situation, ALL the expressions in 'boolList' are calculated,
even though the first may be False.  Here's an example in which I
overcome that:

boolList = Hold[Print@#; PrimeQ@#] & /@ Range[3, 1000, 2];
ReleaseHold[And @@ boolList]

3
5
7
9
False

Bobby Treat

-----Original Message-----
From: Carl K. Woll [mailto:carlw at u.washington.edu] 
To: mathgroup at smc.vnet.net
Subject: [mg36731] [mg36721] Re: build-in commutativity

Evgeni,

Note that both "And" and "Or" can return a value without evaluating all
of
their arguments. For example, if the first argument to And is False,
there
is no reason to look at the other arguments. Suppose a user knows that
one
of the arguments is usually false, and so would like to look at that
argument before any of the others, to avoid unneeded computations of the
other arguments. He would put that argument first. If Mathematica turned
around and sorted the arguments (which is what happens when a function
is
orderless), then that argument might end up being evaluated last. If the
arguments take a significant amount of time to compute, then sorting
first
may cause the function to take much longer to evaluate.

At any rate, if the above is not a concern for you, you may always
change
the attributes of And and Or to anything you want.

Carl Woll
Physics Dept
U of Washington

----- Original Message -----
From: "Evgeni Trifonov" <algolog at yandex.ru>
To: mathgroup at smc.vnet.net
Subject: [mg36731] [mg36721] build-in commutativity


> I'm curious, why the built-in boolean functions "And" and "Or"
> aren't commutative?
>
> Attributes/@{Or,And}//ColumnForm
>
> {Flat, HoldAll, OneIdentity, Protected}
> {Flat, HoldAll, OneIdentity, Protected}
>
> However, the arithmetical functions are "Orderless":
>
> Attributes/@{Plus,Times}//ColumnForm
>
> {Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected}
> {Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected}
>
> -------------------
> Evgeni Trifonov
> Vladivostok, Russia
>







  • Prev by Date: Re: Ball Rolling down on Cosh[t] Path
  • Next by Date: Re: Ball Rolling down on Cosh[t] Path
  • Previous by thread: Re: build-in commutativity
  • Next by thread: How to Properly Use Partition?