Re: Argument types
- To: mathgroup at smc.vnet.net
- Subject: [mg5359] Re: [mg5345] Argument types
- From: jpk at apex.mpe.FTA-Berlin.de (Jens-Peer Kuska)
- Date: Thu, 5 Dec 1996 14:50:09 -0500
- Sender: owner-wri-mathgroup at wolfram.com
> From mathgroup-adm at smc.vnet.net Wed Nov 27 15:54:45 1996 > From: Olivier Georg <olivier.georg at studi.epfl.ch> To: mathgroup at smc.vnet.net > To: mathgroup at smc.vnet.net > Subject: [mg5345] Argument types > Organization: EPF Lausanne > Content-Length: 1083 > X-Lines: 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