MathGroup Archive 2003

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

Search the Archive

Re: Mathematica Problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg42094] Re: Mathematica Problem
  • From: "Will Self" <wself at msubillings.edu>
  • Date: Thu, 19 Jun 2003 03:58:57 -0400 (EDT)
  • References: <bcp0ve$7t1$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

"Jai Bharat Patel" <jaibharatpatel at yahoo.com> wrote in message
news:bcp0ve$7t1$1 at smc.vnet.net...
>
> Hi,
>
> Following statement is working when use it directly in
> Mathematica:
>
> In[1]:= ToExpression["a!=b"] /. Thread[{a,b}->{1,2}]
>
> Out[1]=True
>
> But  same statement is not working if I use it from a
> package:
>
> Package:
>
> BeginPackage["MathIA`Test`"]
>
> myTest::usage =
> " getRandomSet [ ] This is test package ";
>
> Begin["`Private`"]
> myTest[ ] :=
> Module[ { temp2 },
>     temp2 = ToExpression["a!=b"]) /.
> Thread[{a,b}->{1,2}] ;
>     Print[temp2];
> ]
>
> End[]
>
> EndPackage[]
>
> output:
>
> In[3]:= <<MathIA`Test`
>
> In[4]:= myTest[]
> a != b
>
> Please let me know where is my mistake.

There may have been a parenthesis missing.  Anyway

myTest[ ] :=
Module[ { temp2 },
temp2 = ( ToExpression["a!=b"]) /. Thread[{a, b} -> {1, 2}] ;
Print[temp2];
]

created within the context that you described, worked for me.


I have some comments.

1.  I cannot imagine any gain in using ToExpression["a!=b"]
instead of simply a != b.  The first thing the evaluator does to
ToExpression["a!=b"] is to change it to a != b, and then assign
values to a and b if any have been given.

2.  Similarly, I see no possible advantage in using
Thread[{a, b} -> {1, 2}]
rather than the more readable
{a->1, b->2}
(Unless maybe this is a miniature version of some larger project.)

So I would write the test simply as
(a != b) /. {a->1, b->2}

3.  I think the appropriate use of empty square brackets is to
indicate to the user that you are defining a function, and that there
will be other definitions of the function that have arguments within
the brackets.  So I would just use the name myTest, not myTest[],
unless there is some compelling reason to do otherwise.

4.  In the definition of myTest, there is no reason to use Module.
Also, in my experience, people often mistakenly use Print, when in
fact what they really need is to return the value, not print it.
With these points in mind, I would use the simpler version
myTest := ( a != b /. {a -> 1, b -> 2})
If in fact you really do want to print the result, not return it,
then
myTest := Print[(a != b /. {a -> 1, b -> 2}]
would do the job.

5.  My final comment: I would urge you to choose a subject heading that at
least hints at the problem that you have.  There is essentially no
informative content in the heading "Mathematica Problem".  After all, this
is the Mathematica group, and most posts are from people with problems of
one sort or another.



  • Prev by Date: RE: Condition vs PatternTest
  • Next by Date: RE: NonlinearFit
  • Previous by thread: Re: Mathematica Problem
  • Next by thread: Condition vs PatternTest