Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


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

Topic: rookie code issue
Replies: 3   Last Post: Mar 21, 2013 12:55 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Bill Rowe

Posts: 1,639
Registered: 3/14/08
Re: rookie code issue
Posted: Mar 21, 2013 12:54 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 3/20/13 at 4:22 AM, robholman@gmail.com wrote:

>Why doesn't the following code return a 2?

>m = Cos; Which[m == Sin, 5, m == Cos, 2]

Notice

In[3]:= Head /@ {Cos, Sin}

Out[3]= {Symbol,Symbol}

So, by using Equal, you are asking Mathematica to determine
whether two symbols without assigned values are equal which it
cannot do.

What you want to use here is SameQ rather than Equal. That is

In[4]:= m = Cos;
Which[m === Sin, 5, m === Cos, 2]

Out[5]= 2





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

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.