Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

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

Search the Archive

Re: Options for a function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg18120] Re: Options for a function
  • From: Allan Hayes <hay at haystack.demon.co.uk>
  • Date: Fri, 18 Jun 1999 00:51:32 -0400
  • References: <7ka6ub$ins@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Virgil Stokes <virgil.stokes at neuro.ki.se> wrote in message
news:7ka6ub$ins at smc.vnet.net...
> The following simple function for testing
> options does not work the way I expect
> (Vers. 3.0.1).
>
>
> Options[g] = {Opt1->BoxCar};
> g[n_,opts___?OptionQ] :=
>    Module[{opt1,ans},
>   opt1 = Opt1 /. {opts} /. Options[g];
>   If[opt1 == BoxCar, ans = yes, ans = no];
>   {n,ans,opt1}
>  ];
>
> If I use
>
>   g[3]
>
> then it returns
>
> {3,yes,BoxCar}
>
> which is of course correct. However, if I use
>
> g[3,Opt1->TEST]
>
> then it returns
>
> {3,ans$7,TEST}
>
> And, this is not what I would expect. In fact, I am
> unable to get the
>
>    ans = no
>
> part of the If statement to execute. Why?
>
> -- Virgil

With g[3, Opt1->Test]  the If part becomes

If[Test == BoxCar, ans$n = yes, ans$n = no];  (* $n becaues of  Module*)

Now, the value of Test == BoxCar is just Test == BoxCar ; so ans$n = yes and
, ans$ = no are not evaluated,
Hence the output seen.

Three ways out of this:
1)  Use === instead of == ; Test === BoxCar evaluates to False
2)  Use TrueQ[Test == BoxCar]    instead of Test == BoxCar; TrueQ[Test ==
BoxCar]    evaluates to False
3)  Use the fourth entry to If, it evaluates when the first entry gives
neither True nor False: thus
       If[Test == BoxCar, ans = yes, ans = no, ans = no]

Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565











  • Prev by Date: Equality of integrals
  • Next by Date: ValueBox ?
  • Previous by thread: Re: Options for a function
  • Next by thread: Re: Options for a function