MathGroup Archive 2006

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

Search the Archive

Checking function syntax

  • To: mathgroup at smc.vnet.net
  • Subject: [mg64555] Checking function syntax
  • From: Chris Rodgers <rodgers at physchem.NOSPAMox.aREMOVEc.uk>
  • Date: Wed, 22 Feb 2006 05:58:39 -0500 (EST)
  • Organization: Oxford University, England
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

How can I make my own functions give an error if they are called with 
the wrong syntax?

For example, if I define this function

In[37]:=
MyFunc1[x_,y_]:=Tuples[{x,y},2]

and then Map MyFunc2 over the result

In[38]:=
Map[MyFunc2,MyFunc1[a,b]]

I get this (correct, desired) output.

Out[38]=
{MyFunc2[{a,a}],MyFunc2[{a,b}],MyFunc2[{b,a}],MyFunc2[{b,b}]}

However, if I inadvertently added a third argument

In[39]:=
Map[MyFunc2,MyFunc1[a,b,c]]

then I get this (correct, but undesired) output

Out[39]=
MyFunc1[MyFunc2[a],MyFunc2[b],MyFunc2[c]]

I would prefer to get some sort of error message in the second case, 
such as the built-in function Sin[...] produces when called with the 
wrong number of arguments.

In[41]:=
Sin[1.]
Out[41]=
0.841471
In[42]:=
Sin[2,3]
 From In[42]:=
Sin::argx : Sin called with 2 arguments; 1 argument is expected. Out[42]=
Sin[2,3]

Many thanks,

Chris.


  • Prev by Date: General--Making the DisplayFormula style in ArticleModern look like Traditional
  • Next by Date: Surface graphing question
  • Previous by thread: Re: General--Making the DisplayFormula style in ArticleModern look like Traditional
  • Next by thread: Re: Checking function syntax