[Date Index]
[Thread Index]
[Author Index]
Re: conditional pattern question
*To*: mathgroup at smc.vnet.net
*Subject*: [mg15693] Re: conditional pattern question
*From*: "Allan Hayes" <hay at haystack.demon.co.uk>
*Date*: Mon, 1 Feb 1999 14:54:13 -0500 (EST)
*References*: <7913bm$e3d@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
Original posting beneath **************************************
David,
I don't have the answer, but here are some observations and a guess.
We also have
Clear[h,a];
h[x_/;Print[a=1]]:= 1
a
a
h[x_/;Print[a=2]]:= 2
1
a
1
h[x_/;Print[a=3]]:= 3
1
2
a
2
My guess is that this is to do with deciding whether to store a new rule
or replace the existing one. In fact the decision seems to be taken on
just syntactical evidence.
?h
"Global`h"
h[x_ /; Print[a = 1]] := 1
h[x_ /; Print[a = 2]] := 2
h[x_ /; Print[a = 3]] := 3
The following is understandable as the result of testing for the
applicability of the two rules
h[x];
1
2
3
a
3
The escaping of definions from a test could cause problems.
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
***********************************************************************
David Reiss wrote in message <7913bm$e3d at smc.vnet.net>...
>
>I don't understand the following behavior: could someone educate me
>here? ...or is it a bug?
>
>First clear the functions and variables.
>
>
>In[1]:=
>ClearAll[NumericWithMessageQ,f];
>
>
>Now define a message:
>
>
>In[2]:=
>Test::testmessage="The argument `1` has generated this message.";
>
>
>Introduce a boolian test that has the added side effect of generating a
>message when the test yields False (I am using NumericQ as an example,
>but the problem that I am illustrating is independent of this):
>
>
>In[3]:=
>NumericWithMessageQ[z_]:=
> If[NumericQ[z],
> True,
> Message[Test::testmessage,z];False]
>
>
>Now give a function definition for numeric values of the argument
>
>
>In[4]:=
>f[y_?NumericWithMessageQ]:=1;
>
>
>Also give a definition for all other cases: the message defind a bove is
>generated when this is executed-- I didn't expect this to happen. That
>the message is generated at this point puzzels me. I don't understand
>why this happens.
>
>
>In[5]:=
>f[y_]:=2;
>
>Test::"testmessage": "The argument \!\(y_\) has generated this message."
>
>
>Note though that the function works properly. Here is a numeric case:
>
>
>In[6]:=
>f[4]
>
>Out[6]=
>1
>
>
>And here is a non-numeric case (and the message is generated as per the
>design).
>
>
>In[7]:=
>f[a]
>
>Test::"testmessage": "The argument \!\(a\) has generated this message."
>
>Out[7]=
>2
>
>
>If the function is specified as follows, the message is not generated
>unexpectedly when the function is defined.
>
>
>In[8]:=
>ClearAll[f]
>
>In[9]:=
>f[y_]/;NumericWithMessageQ[y]:=1;
>
>In[10]:=
>f[y_]:=2;
>
>
>And the function works as above.
>
>
>In[11]:=
>f[4]
>
>Out[11]=
>1
>
>In[12]:=
>f[a]
>
>Test::"testmessage": "The argument \!\(a\) has generated this message."
>
>Out[12]=
>2
>
>--
>
>
>
>---------------------------------------- Scientific Arts:
>Creative Services and Consultation
>for the Applied and Pure Sciences
>
>David Reiss
>Email: dreiss at !SPAMscientificarts.com
>----------------------------------------
>
>
>
>Remove the !SPAM to send email
>
Prev by Date:
**Re: conditional pattern question**
Next by Date:
**Re: 4th order DE, NDSolve no solution, why?**
Previous by thread:
**Re: conditional pattern question**
Next by thread:
**no kernel?**
| |