Message Boards Message Boards


How to construct a function with multiple conditions, that returns a list?

Posted 10 years ago
5 Replies
0 Total Likes
Hello guys, I started learning Mathematica quite recently and I'm amazed by the possibilities of this software! Despite my genuine enthusiasm, I recently encountered a barrier (probably like most of users at the beginning). To most of You my problem surely will be trivial, but it is quite a headache for me. The case is a bit complicated so I will simplify it, so that I can just get an Idea how to get started and then I can develop the code without bothering You too much. Here it is - I would like to write a function that would process a list, with let's say 3 elements, and also return a list with the same number of elements. I guess the beginning should look like this:

The next step should be to check which elements of the input list are numbers - if a and b are numbers and c is string (for instance a letter) the function returns {a,b,a+b+1}, if b and c are numbers, the function returns {b+c-1,b,c} and if a and c are numbers, the function returns {a,2*(a+c),c}. If there are less than 2 numbers in the list, the function should return the original list. 

I know it seems easy, but I have still just a little knowledge about Mathematica language. I assume that I should use If[] and NumberQ but don't know how to put them together. Sorry I don't enclose any code, but all my efforts ended with error messages or a disaster. I also couldn't find any similar example in any of the tutorials or books I read. 

Thanks for Your help and every piece of advise You are ready to share!
POSTED BY: Julia Kleinfeld
5 Replies
Many patterns can be gathered into one structure with a Switch statement,
function1[{a_, b_, c_}] :=
Switch[{a, b, c},
  {_?NumberQ, _?NumberQ, _String}, {a, b, a + b + 1},
  {_String, _?NumberQ, _?NumberQ}, {b + c - 1, b, c},
  {_?NumberQ, _String, _?NumberQ}, {a, 2*(a + c), c},
  {_, _, _}, {a, b, c}]
In[ ]:= function1[{10, 20, "30"}]

Out[ ]= {10, 20, 31}
In[ ]:= function1[{"10", 20, "30"}]

Out[ ]= {"10", 20, "30"}
Pattern matching allows you to define the same function in multiple ways and you only need to apply it once.
POSTED BY: Frank Kampas
Posted 10 years ago
f[{a_?NumberQ, b_?NumberQ, c_?StringQ}] := {a, b, a + b + 1};
...details for you to fill in and then include a fourth and final function...
f[v_] := v;
Map[f, {{1, 2, "s"}, {"r", 2, 3}, {5, "v", 7}, {9, 8, 7}, {"a", "b", 4}, {"c", "d", "e"}}]

Fill in the details, run it, then study the result and read enough of the documentation to figure out why it did what it did.
That last step is by far the most important part of this, don't just get someone to give you the answer to this part.
POSTED BY: Bill Simpson
Posted 10 years ago
Thanks for the answer Frank. Surely it works, but I would have to write three such functions and then apply all of them to my list, which would result in one "proper" output and two "original" lists as outputs of those two functions with unmatched patterns. Is there a way to do it as one function, which would generate only one "desired" outcome?
POSTED BY: Julia Kleinfeld
f[{a_?NumberQ, b_?NumberQ, c_String] = {a, b, a + b + 1}

POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract