Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #13900 > unrolled thread

rookie code issue

Started byrobholman@gmail.com
First post2013-03-20 08:22 +0000
Last post2013-03-21 04:55 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  rookie code issue robholman@gmail.com - 2013-03-20 08:22 +0000
    Re: rookie code issue Daniel Huber <danielphuber@gmail.com> - 2013-03-21 04:54 +0000
    Re: rookie code issue David Bailey <dave@removedbailey.co.uk> - 2013-03-21 04:55 +0000

#13900 — rookie code issue

Fromrobholman@gmail.com
Date2013-03-20 08:22 +0000
Subjectrookie code issue
Message-ID<kibrkj$156$1@smc.vnet.net>
Hi folks:

I'm working on a bit of code in which I want students to choose a trig function from a drop-down menu, and then from there have some choices for viewing the graph. All is working fine except for one thing. There are a couple of constants that depend on the function they choose from the menu, and I'm trying to define the constant based on their choice for f. The basic issue I'm having boils down to the following question:

Why doesn't the following code return a 2?


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



Thanks for the help!

Rob

[toc] | [next] | [standalone]


#13901

FromDaniel Huber <danielphuber@gmail.com>
Date2013-03-21 04:54 +0000
Message-ID<kie3qn$5ur$1@smc.vnet.net>
In reply to#13900
Hi,
use SameQ (===) instead of Equal (==) to compare variables.
Daniel

On Wednesday, March 20, 2013 9:22:43 AM UTC+1, Rob Holman wrote:
> Hi folks:
>
>
>
> I'm working on a bit of code in which I want students to choose a trig function from a drop-down menu, and then from there have some choices for viewing the graph. All is working fine except for one thing. There are a couple of constants that depend on the function they choose from the menu, and I'm trying to define the constant based on their choice for f. The basic issue I'm having boils down to the following question:
>
>
>
> Why doesn't the following code return a 2?
>
>
>
>
>
> m = Cos;
>
> Which[m == Sin, 5, m == Cos, 2]
>
>
>
>
>
>
>
> Thanks for the help!
>
>
>
> Rob

[toc] | [prev] | [next] | [standalone]


#13903

FromDavid Bailey <dave@removedbailey.co.uk>
Date2013-03-21 04:55 +0000
Message-ID<kie3rt$62u$1@smc.vnet.net>
In reply to#13900
On 20/03/2013 08:22, robholman@gmail.com wrote:
> Hi folks:
>
> I'm working on a bit of code in which I want students to choose a trig function from a drop-down menu, and then from there have some choices for viewing the graph. All is working fine except for one thing. There are a couple of constants that depend on the function they choose from the menu, and I'm trying to define the constant based on their choice for f. The basic issue I'm having boils down to the following question:
>
> Why doesn't the following code return a 2?
>
>
> m = Cos;
> Which[m == Sin, 5, m == Cos, 2]
>
>
>
> Thanks for the help!
>
> Rob
>
What you get back is:

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

The problem is that a comparison that resolves to a symbol match - like 
x==y is neither True nor False, but undecided until x and y get values - 
*unless* the two symbols are the same - x==x. This leaves the whole 
Which construct unevaluated!

You need structural comparisons:

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

or alternatively use Switch:

Switch[m, Sin, 5, Cos, 2]


David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web