Re: Which, If and "neither True nor False"
- To: mathgroup at smc.vnet.net
- Subject: [mg9728] Re: Which, If and "neither True nor False"
- From: "Paul E Howland" <PEHOWLAND at dra.hmg.gb>
- Date: Tue, 25 Nov 1997 00:06:25 -0500
- Organization: Defence Research Agency
- Sender: owner-wri-mathgroup at wolfram.com
Will Self wrote in message <653eac$6ul at smc.vnet.net>... >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. My particular problem involved a function defined like the following: f[x_]:=Module[{x2}, Which [x<10, x2=10, True, x2=x ]; (do stuff with x2) ] Which worked fine when x was defined, but when x was undefined (eg. just a symbol), x2 remained undefined, as the Which statement was returned unevaluated. I thus wanted a condition which would allow me to take an action when x<10 was neither True nor False. The most comprehensive implementation of Which (suggested in email to me by Robert Villegas of WRI ) would be to have a construct of the form: Which[test1, {action if True, action if Indeterminate}, test2, {action if True, action if Indeterminate}, ... True, default action ]; which allows you to define an action to be carried out if a test returns Indeterminate. This action could be to continue to the next test. An alternative, less powerful, but still useful adaptation of Which would be to have a construct of the form: Which[test1, action if True, test2, action if True, True, default action if test1 and test2 are False, Indeterminate, action if test1 or test2 are Indeterminate ]; This at least allows you to determine whether a test was True, False, or Indeterminate, whereas all the solutions offered so far do not distinguish between Indeterminate results and False results. A possible implementation of the above function is shown below. I have defined it recursively: myWhich[a__] := Module[{args, indet, f}, args=Partition[{a}, 2]; indet=Last[args][[2]]; args=Drop[args, -1]; f[i_,args_] := If[ReleaseHold[args[[i, 1]]], ReleaseHold[args[[i,2]]], f[i+1, args], ReleaseHold[indet]]; f[1, args] ] In the simple implementation above, myWhich assumes that the penultimate condition is always True, and the ultimate condition is always Indeterminate. It needs refining to do this properly. Also, at the moment, you have to wrap Hold around each argument to myWhich: I though you could use SetAttributes[myWhich, HoldAll] to this automatically, but I can't seem to manage it! Call the function as follows: myWhich[ Hold[a<b], Hold[Print["a<b"]], Hold[a>c], Hold[Print["a>c"]], Hold[True], Hold[Print["True"]], Hold[Indeterminate], Hold[Print["Indeterminate"]] ] Depending on whether a,b and c are defined or not, the function will behave as I have described. Paul