RE: Error Testing a List on input
- To: mathgroup at smc.vnet.net
- Subject: [mg37693] RE: [mg37685] Error Testing a List on input
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Sat, 9 Nov 2002 00:28:42 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: flip [mailto:flip_alpha at safebunch.com] To: mathgroup at smc.vnet.net >Sent: Friday, November 08, 2002 8:16 AM >To: mathgroup at smc.vnet.net >Subject: [mg37693] [mg37685] Error Testing a List on input > > >Hello, > >I was wondering if there is an easy way to do this. > >I want to input a list of nine numbers. I would like to test >each element in >the list prior to acting upon it. > >This ends up being a sort of error check on the list prior to >manipulating >it, if it is wrong, produce an error message or do nothing >with the module. > >So, I would like to test each element to be a. an integer, b. >>= 0, c. <= 9. > >Is there a way to test this list at the input prior to acting upon it? > >Thanks, Flip > > > I don't understand your use of input, and so this might be of no help. But we have pattern matching and Condition for checking arguments, e.g. In[9]:= Remove[f] In[10]:= f[a:{__Integer}] /; And @@ Thread[0 <= a <= 9] := a^2 In[11]:= f[Range[0, 9]] Out[11]= {0, 1, 4, 9, 16, 25, 36, 49, 64, 81} In[12]:= f[{-1, 5}] Out[12]= f[{-1, 5}] In[13]:= f[{9, 10}] Out[13]= f[{9, 10}] In[14]:= f[3] Out[14]= f[3] In[15]:= f[{3, {3}}] Out[15]= f[{3, {3}}] You may separate function and testing completely if you like: In[24]:= Remove[f, test] In[25]:= f[a_] /; test[a] := a^2 In[26]:= test[a_List /; VectorQ[a, Head[#] == Integer &]] := And @@ Thread[0 <= a <= 9] Such you may vary the tests whenever you like. -- Hartmut Wolf