MathGroup Archive 1997

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

Search the Archive

Re: Which, If and "neither True nor False"

  • To: mathgroup at smc.vnet.net
  • Subject: [mg9709] Re: [mg9652] Which, If and "neither True nor False"
  • From: wself at viking.emcmt.edu (Will Self)
  • Date: Fri, 21 Nov 1997 01:31:58 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

Paul Howland wrote:

>I've just stumbled across a bug in some old code of mine, in which I had
>a statement of the form:
>
>Which[
>        a<b, DoSomething[],
>        a>c, DoSomethingElse[],
>        True, DoThis[]
>        ]
>
>which I assumed would call DoThis[] if neither a<b nor a>c were True.
>However, what I didn't realise is that if a is undefined, then the
>first two conditions are neither True nor False, and Mathematica
>returns the whole Which statement unevaluated!
>
>The If statement has an optional fourth argument which is executed if
>the test returns neither True nor False.  Is it possible to do the same
>in a Which statement?  If not, why not?!


I don't see how it could be useful to have a Which statement that allows
the optional fourth argument as in the If statement.  How would you
want it to behave?  Suppose that the first test evaluates to neither
True nor False, and that a fourth argument is given there.  Would you
want Which to return the fourth argument, or to look further for a test
that evaluates to True?  You could perhaps have your Which look through
all the tests and return the value associated with the first True, and
then, if there aren't any Trues or Falses, to return the first of the
"fourth arguments", but that hardly seems like something useful.

In the example you gave, you might want to do something like this:

Which[
        (a<b)===True, DoSomething[],
        (a>c)===True, DoSomethingElse[],
        True, DoThis[]
        ]

Will Self
Montana


  • Prev by Date: Re: Re: [mg9568}Color Printing -- Thanks
  • Next by Date: Re: Which, If and "neither True nor False"
  • Previous by thread: Which, If and "neither True nor False"
  • Next by thread: Re: Which, If and "neither True nor False"