2
$\begingroup$

when I have an expression, that is unknown to mathematica, it is returned exactly the same, as it was called.

For example SeeFlyingPigs[1,2] called on the line cames out as SeeFlyingPigs[1,2].

This is completely fine.

I would like to write an IF, that would cause this behaviour, if there comes integer and a double as inputs, there would be an implementation specified and user would be presented with a result SeeFlyingPigs[1, 5.3] output for example 31,

But in all other cases, I want user to be returned exactly what he put in.

Till now, I tried Unevaluated[Hold[{SeeFlyingPigs[a, b]}] - that is closest as I got, but still, the output is Hold[{SeeFlyingPigs[times[x, 2]]}]. That is not EXACTLY what the user typed in, which was SeeFlyingPigs[6, 66], without the Hold, recursion is ran... (not good).

I have even tried bunch of other things, but cannot get user be given what he put in, like when no implementation is provided.

I do not want to do this by specifying the input types SeeFlyingPigs[a_Integer, b_Double] is not what I want (for bunch of reasons).

Thanks for any help, tips, and tricks.

$\endgroup$
6
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Commented Dec 21, 2014 at 17:26
  • $\begingroup$ You can add a Condition to your definition. Could you explain why you don't want to use _Integer and _Real patterns? $\endgroup$
    – jkuczm
    Commented Dec 21, 2014 at 17:56
  • $\begingroup$ @jkuczm Thank you, apparently, Condition is what I was looking for. Could you post it as answer, so I can accept it? Helped a lot! $\endgroup$
    – jmodrak
    Commented Dec 21, 2014 at 17:59
  • $\begingroup$ Depending on what precisely you put in Condition your function can behave exactly like function with SeeFlyingPigs[a_Integer, b_Real] definition and the latter is more idiomatic in Mathematica. Without knowing your "bunch of reasons" for not using it, I don't really know whether it's good advice to use Condition. $\endgroup$
    – jkuczm
    Commented Dec 21, 2014 at 18:36
  • $\begingroup$ Ok, maybe I am just too new to Mathematica, as I recognize with every line I have to rewrite. Still, Condition works good, as I want it to. This was my today's aim, later, I might recognize, I am wrong right now. But only by recognizing my mistakes on my own, I will be able to learn more. For now, I would leave it as is, thanks Jakub. $\endgroup$
    – jmodrak
    Commented Dec 21, 2014 at 18:48

1 Answer 1

5
$\begingroup$

Arbitrary expressions are returned verbatim unless there are evaluation rules associated with them in the relevant tables. (See e.g. DownValues, UpValues, etc.) These rules are generally known as definitions. Only if the expression matches the pattern of a definition (including internal evaluation rules) will something else be returned.

An example:

foo[_Integer] := "Integer definition matched."

foo["seven"]

foo[3.14, Pi]

foo[16]
foo["seven"]

foo[3.14, π]

"Integer definition matched."

Evaluation can take place as part of the testing of a definition pattern without the pattern being considered a match. For example using Condition:

foo[x_] /; (Print["I am testing an argument: ", x]; x == 3) := "Three definition matched"

foo[7.2]

During evaluation of In[]:= I am testing an argument: 7.2

foo[7.2]
foo[3.00]

During evaluation of In[]:= I am testing an argument: 3.

"Three definition matched"

For more on this topic related to Message please see:

$\endgroup$
1
  • 1
    $\begingroup$ Thanks a lot. My code works now :) Thankful to all StackExchange members, wish you all the very best Christmas time! $\endgroup$
    – jmodrak
    Commented Dec 22, 2014 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.