I need to use If but with only one option that is if "a" then do "b", else do nothing. So I wrote If[a,b] but the problem is that if it is not a it returns Null in my output.
How to avoid this?
Here is the specific example I was working on.
I am looking for the number of comparisons performed by the quick sort algorithm. Here is the code from Rosetta code with my additions
QuickSort[x_List] :=
Module[{pivot, aa = 0, bb = 0}, If[Length@x <= 1, Return[x]];
pivot = First[x];
aa = If [Length[Cases[x, j_ /; j < pivot]] > 1,
Length[Cases[x, j_ /; j < pivot]] - 1 , Sequence[]];
bb = If [Length[Cases[x, j_ /; j < pivot]] > 1,
Length[Cases[x, j_ /; j > pivot]] - 1 , Sequence[]];
count = count + aa + bb;
Flatten@{QuickSort[Cases[x, j_ /; j < pivot]],
Cases[x, j_ /; j == pivot],
QuickSort[Cases[x, j_ /; j > pivot]]} ; Return[count] ]
now if you run QuickSort[{4, 3, 2, 1, 5}] you will get 2+2 Null instead of 4



Nullis usually no problem, the FrontEnd will usually not even create an output cell for that return value... – Albert Retey Mar 30 '12 at 8:43