Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.



Re: [mg5345] Argument types
Posted:
Dec 1, 1996 11:20 PM


> From mathgroupadm@smc.vnet.net Wed Nov 27 15:54:45 1996 > From: Olivier Georg <olivier.georg@studi.epfl.ch> > To: mathgroup@smc.vnet.net > Subject: [mg5345] Argument types > Organization: EPF Lausanne > ContentLength: 1083 > XLines: 22 > > Hi, > > I recently disovered that you could specify the type of the arguments > of a function being defined. For exemple, f[x_Integer] if you want x > to be an integer. There's also List, Real, Complex and Symbol. But > there's also ?MatrixQ which is not in the Mathematica book, to specify > a matrix. Are there other types? > > Olivier Hi Olivier,
You *must* decide between a pattern with a given head like _Integer, _Rational, _Plus and a test like _?VectorQ and _?MatrixQ. The first one refers to a expression with a certain head where _?MatrixQ refers to a test MatrixQ[expr]. The aquivalent to
f[x_?MatrixQ]:= ..
is
f[x: {{__}..}] /; Equal @@ (Length /@ x):= ...
because the pattern {{__}..} say that You what a list of lists with at least one element in the sublists, the appended condition says that You only what lists of lists with equal length.
Since MatrixQ is a test and not a (you say type but is a) head You can add any function with one argument to test the pattern. Beside EvenQ, PolynomQ in
f[x_?EvenQ] := ..
you can invent Your own tests like
f[x_?(Head[#]==List && EvenQ[Length[#]] &)] := to test if x is a list with a even number of arguments.
There is probably a infinite number of such possible tests and to list them all in the Mathematica book will increase the sufficiently.
Hope that helps Jens



