MathGroup Archive 2013

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

Search the Archive

Re: rookie code issue

  • To: mathgroup at
  • Subject: [mg130217] Re: rookie code issue
  • From: David Bailey <dave at>
  • Date: Thu, 21 Mar 2013 00:57:10 -0400 (EDT)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • References: <kibrkj$156$>

On 20/03/2013 08:22, robholman at 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

  • Prev by Date: Re: rookie code issue
  • Next by Date: Re: rookie code issue
  • Previous by thread: Re: rookie code issue
  • Next by thread: CDF help