The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Topic: Re: [mg5345] Argument types
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Jens-Peer Kuska

Posts: 12
Registered: 12/7/04
Re: [mg5345] Argument types
Posted: Dec 1, 1996 11:20 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

> From Wed Nov 27 15:54:45 1996
> From: Olivier Georg <>
> To:
> 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

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2017. All Rights Reserved.