Re: Conditionals with multiple tests?

• To: mathgroup at smc.vnet.net
• Subject: [mg24122] Re: Conditionals with multiple tests?
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Wed, 28 Jun 2000 02:11:43 -0400 (EDT)
• References: <8j9dvs\$523@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

"A. E. Siegman" <siegman at stanford.edu> wrote in message
news:8j9dvs\$523 at smc.vnet.net...
> Let's say I want to assign values to three variables p1, p2, p3 that
> will depend on five different (and nonoverlapping) tests test1 to test 5.
>
> One way to do this is obviously
>
>       p1 = Which[test1, value11, test2, value12, . . . ]
>       p2 = Which[test1, value21, test2, value22, . . . ]
>       p3 = Which[test1, value31, test2, value32, . . . ]
>
> But a more compact and (for me anyway) neater approach is
>
>    Which[test1, p1=value11; p2=value21; p3=value31,
>               test2, p1=value21; p2=value22; p3=value32,
>               test3, . . .
>               test4, . . .
>               test5, . . . ]
>
> Is this form legal?  That is, can one use:
>
>       Which[test1, expr1, test2, expr2, . . .]
>
> where expr1, expr2, . . . may be compound expressions?

A compound expression is a perfectly legitimate expression, so no problem
there.

Assuming that no definitions produce side effects.
If all of your tests give either True or False and at least one gives True,
then the two ways will be equivalent. But otherwise they may not be
equivalent:

Which[test1, v1, test2, v2 ... testn, vn] seems to evaluate as follows.
For i = 1,2,...in  turn
? evaluate testi to testi*
? if testi* is True return the value of vi
? if testi* is False delete testi and vi
? if testi* is neither True nor False return the current Which[testi*,
vi..]

? if Which[] is reached return Null.

Check

Clear["`*"]
F = False; T = True; U = vU; a = va; b = vb;

{Which[F,a,T,b],
Which[F,a,U,b,T,b],
Which[F,a,F,b],
Which[]
}

{vb, Which[vU, b, T, b], Null, Null}

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"A. E. Siegman" <siegman at stanford.edu> wrote in message
news:8j9dvs\$523 at smc.vnet.net...
> Let's say I want to assign values to three variables p1, p2, p3 that
> will depend on five different (and nonoverlapping) tests test1 to test 5.
>
> One way to do this is obviously
>
>       p1 = Which[test1, value11, test2, value12, . . . ]
>       p2 = Which[test1, value21, test2, value22, . . . ]
>       p3 = Which[test1, value31, test2, value32, . . . ]
>
> But a more compact and (for me anyway) neater approach is
>
>    Which[test1, p1=value11; p2=value21; p3=value31,
>               test2, p1=value21; p2=value22; p3=value32,
>               test3, . . .
>               test4, . . .
>               test5, . . . ]
>
> Is this form legal?  That is, can one use:
>
>       Which[test1, expr1, test2, expr2, . . .]
>
> where expr1, expr2, . . . may be compound expressions?
>
> (I would say that The Mathematica Book is not at all clear on this
> point, as regards either Which[] or If[].)
>
> If not, is there a legal way to implement the basic objective?
>

• Prev by Date: Re: Conversion of Orderless functions to non Orderless one
• Next by Date: Re: Piecewise functions definition and usage
• Previous by thread: Re: Conditionals with multiple tests?
• Next by thread: Piecewise functions definition and usage