RE: Which, If and "neither True nor False"
- To: mathgroup at smc.vnet.net
- Subject: [mg9672] RE: [mg9652] Which, If and "neither True nor False"
- From: jmthomas <jmthomas at cybercable.tm.fr>
- Date: Fri, 21 Nov 1997 01:31:10 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Sorry, my answer was too fast:
let:
f[a_,b_,c_]:=Which[a<b,Print["first case"],a>c,Print["second
case],True,Print["third case"]
If you try to evaluate f with numerical values for a, b and c, you will
get an answer (a print message). But in the case where a, b and c
remain symbolic, there is no way to decide which of the tests will be
true, as far as neither the first not the second test return false,
and then the which command returns unevaluated.
So the good answer is:
f[a_,b_,c_]:=Which[TrueQ[a<b],Print["first
case],TrueQ[a>c],Print["second case"],True,Print["unknown case"]]
TrueQ evaluates always to a boolean value, so the unknown case can be
reached.
----------------------------------------------- Jean-Marie THOMAS
Conseil et Audit en Ingenierie de Calcul jmthomas at cybercable.tm.fr
+33 (0)3 88 32 93 64
www.cybercable.tm.fr/~jmthomas
=======================
-----Original Message-----
From: Paul E Howland [SMTP:PEHOWLAND at dra.hmg.gb] To:
mathgroup at smc.vnet.net
Sent: Tuesday, November 18, 1997 1:50 AM To: mathgroup at smc.vnet.net
Subject: [mg9652] Which, If and "neither True nor False"
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?!
Paul
Paul E Howland PhD MEng CEng MIEE Room BY209 Senior Scientist
DERA (Malvern) Land Systems Sector St Andrews Road
Defence Evaluation & Research Agency Malvern tel.
+44-(0)1684-895767 Worcestershire fax.
+44-(0)1684-896315 UK Email PEHowland at dera.gov.uk Web
Site http://www.dera.gov.uk
Official Disclaimer:
The views expressed above are entirely those of the writer and do not
represent the views, policy or understanding of any other person or
official body.