Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1996

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

Search the Archive

Re: Argument types

  • To: mathgroup at
  • Subject: [mg5359] Re: [mg5345] Argument types
  • From: jpk at (Jens-Peer Kuska)
  • Date: Thu, 5 Dec 1996 14:50:09 -0500
  • Sender: owner-wri-mathgroup at

> From mathgroup-adm at Wed Nov 27 15:54:45 1996
> From: Olivier Georg <olivier.georg at>
To: mathgroup at
> To: mathgroup at
> 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]:= ..


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

  • Prev by Date: Re psrender type 11 error
  • Next by Date: Linux vs. Win95 versions of MMA
  • Previous by thread: Re: Argument types
  • Next by thread: Re: Argument types