Re: Checking the form of an option
- To: mathgroup at smc.vnet.net
- Subject: [mg21187] Re: Checking the form of an option
- From: Bojan Bistrovic <bojanb at physics.odu.edu>
- Date: Fri, 17 Dec 1999 01:23:47 -0500 (EST)
- Organization: Old Dominion Universityaruba
- References: <831k39$dju@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
DIAMOND Mark wrote:
>
> I want to check whether option "K" to a function is an integer, a list of
> single numbers, or neither of these, and then to execute one of three
> different statements.
>
> If these K were a simple parameter, I could do it with
> f[k_Integer] := functionBody;
> f[k:{_Integer}..] := functionBody;
> f[k_] := functionBody
>
> but I cannot see how to check for similar conditions on an *option* inside
> one function. Can anyone help?
It depends on the number of arguments and a few other things. In
general, let's call the function "f". If you have lets say exactly one
argument to the function, then you'd use something like
f[aa_, ooo___Rule]:= function1 /; IntegerQ[(K /. {ooo} /. Options[f])]
f[aa_, ooo___Rule]:= function2 /; (Head[#]===List &&
IntegerQ[#[[1]]])&[K /. {ooo} /. Options[f]]
f[aa_, ooo___Rule]:= function3
As long as you have a (small) finite number of argumets, you just
replace aa_ with a list of your arguments. If you have a function with
a variable number of arguments, you'd have to use somethin like (as long
as you have at least one argument before the options)
f[aaa__ /; Not[MemberQ[{aaa},_Rule]], ooo___Rule]:= ...
If you DON'T have any arguments before the options you'd have to
substitute aaa__ with aaa___ . You could use always "aaa" with three
blanks, but it will increase the risc of infinite loops and it will slow
down your code (believe me, I've tried). I hope this helps.
--
-------------------------------------------------------------
Bojan Bistrovic, bojanb at physics.odu.edu
Old Dominion University, Physics Department, Norfolk, VA
-------------------------------------------------------------