Re: Functions and evaluation, returning themselves
- To: mathgroup at smc.vnet.net
- Subject: [mg7265] Re: Functions and evaluation, returning themselves
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Tue, 20 May 1997 02:59:15 -0400 (EDT)
- Organization: University of Western Australia
- Sender: owner-wri-mathgroup at wolfram.com
Red wrote:
> I am trying to write a function in Mathematica
> that can give a message and return the
> expression entered.
>
> For example
> In[1] := Sin[]
>
> ``Sin::"argx":
> "Sin called with 0 arguments; 1 argument is expected.
>
> Out[1]= Sin[]
The following rule issues an error message (using the built-in Message
mechanism) if f is called with an incorrect number (i.e. != 1) of
arguments:
f[args___]:= Null/; Length[{args}] != 1 &&
Message[f::"argx",f,Length[{args}]]
As a side-effect of evaluating the Condition -- entered using the infix
shorthand /; which can be read as such that -- the rule on the
right-hand side computes the number of arguments using Length and
(denoted here by &&) prints a message when it is not equal to one (i.e.,
Length[{args}] != 1):
f[1,2]
f::"argx": "f called with 2 arguments; 1 argument is expected."
f[1,2]
Cheers,
Paul
_________________________________________________________________
Paul Abbott
Department of Physics Phone: +61-8-9380-2734
The University of Western Australia Fax: +61-8-9380-1014
Nedlands WA 6907 paul at physics.uwa.edu.au
AUSTRALIA http://www.pd.uwa.edu.au/Paul
God IS a weakly left-handed dice player
_________________________________________________________________