1

I am quite new to Mathematica and have an issue regarding which statement.

cut[x_, y_] := which[x>y, doFunctionX[], y>x, doFunctionY[]]
cut/@{X,Y}]

I wish to display only the true case. However, I am unable to find an operation to do it.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • 2
    NOt really sure what you're trying to do with the last line, since X and Y are not numbers (and it should be cut[X, Y] anyway). But, if you change which to Which (all built-in functions in Mathematica are capitalized), your code should be fine. When you change that, cut[5, 3] evaluates to doFunctionX[]. – march Nov 01 '19 at 15:35
  • 1
    Here is a good post with common pitfalls for new users. I anchored it to your issue (Mathematica is case sensitive and the built-in system functions always start with a capital letter): https://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/18395#18395 – Arnoud Buzing Nov 01 '19 at 15:52
  • You may also want to consider handling the case when x == y. – Rohit Namjoshi Nov 01 '19 at 17:54
  • Thank you. The issue was due to not capitalizing the first letter. – Siddeshwar Raghavan Nov 02 '19 at 16:36

1 Answers1

2

In Mathematica you can do this with function name overloading. Like so:

cut[x_, y_] /; x > y := doFunctionX[]
cut[x_, y_] /; x < y := doFunctionY[]

where the Condition (/;) operator enforces the conditions you want to place on the arguments.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257