Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i'm struggling to implement the following parametric function:

I need to create a g function with two arguments (w and v). If w and v are not empty lists, then it will return a list containing two other lists: - one containing the elements of w that do not occur in v, - and the other containing the elements of v that do not occur in w, If one of w or v are empty lists, it should return a message "One of the lists is empty".

Hope anyone can help me out here, thanks in advance.

share|improve this question
Seems quite straightforward. Check the docs for If[] and Complement[] – Dr. belisarius Jan 9 '15 at 17:37
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! – Dr. belisarius Jan 9 '15 at 17:40
Try g[w_, v_] := If[w == {} || v == {}, "D'oh!", {Complement[w, v], Complement[v, w]}]. – DumpsterDoofus Jan 9 '15 at 17:43
I added my comment as an answer. If people think this question is trivial enough to be closed, I'll delete my answer and vote to close for "easily found in the documentation" and leave my answer as the comment above. – DumpsterDoofus Jan 9 '15 at 18:00
Regardless, the title is unrelated to the question (which has nothing to do with parametric functions). If this question is to remain, the title should be changed to reflect the actual question. – David G. Stork Jan 9 '15 at 20:16

Try the following:

g[w_, v_] := If[w == {} || v == {}, "D'oh!", {Complement[w, v], Complement[v, w]}]

Some tests:

g[{2, 3, 4}, {2, 5}]
g[{2}, {}]


{{3, 4}, {5}}
share|improve this answer

Here is an alternative, using dispatch between multiple definitions:

g[{},_] = g[_,{}] = "One of the lists is empty";
g[a_,b_] := { a~Complement~b, b~Complement~a };

If you like syntactic sugar, you could also write the second line as

g[a_,b_] := Complement @@@ { {a,b}, {b,a} };
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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