# How to avoid returning a Null if there is no “else” condition in an If contruct

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

-
What is the problem with that, returning Null is 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
ok i my case i could just add 0, in the "else" case. but i wonder why proposal of "kguler" and "ruebenko" that i also tested didn't work in my case – Raph Mar 30 '12 at 9:33

## 7 Answers

It depends what you consider nothing, but you could try something like this

If[a, b, Unevaluated[Sequence[]]]


for example

3 + If[False, 1, Unevaluated[Sequence[]]]


returns 3. Wrapping an argument of a function in Unevaluated is effectively the same as temporarily setting the attribute Hold for that argument meaning that the argument isn't evaluated until after it's inserted in the definition of that function.

By the way, in your definition of QuickSort you're calling Cases[x, j_ /; j < pivot] six times. It's probably more efficient to assign Cases[x, j_ /; j < pivot] to a dummy variable and use that instead.

-
Cases[x, j_ /; j < pivot] 4 times, Cases[x, j_ /; j == pivot] and Cases[x, j_ /; j > pivot] each once. But, a variable is definitely needed. – rcollyer Mar 30 '12 at 11:24
@rcollyer you're right, I was a bit hasty in scanning the code. – Heike Mar 30 '12 at 11:36
Unevaluated[] == Unevaluated[Sequence[]]? – chyaong Jul 19 '13 at 6:43
@chyanog I believe this is true: one may use Unevaluated[] in any place were Unevaluated[Sequence[]] is used. I did not recommend it because ##&[] is shorter, but it should be understood that ##&[] and Unevaluated[] behave differently, though both are appropriate for this problem. I added a section to my answer that hopefully explains this. – Mr.Wizard Aug 13 '13 at 1:13

The semantics for if is If[cond,t,f] if f (False) is not given Null is returned.

I am guessing that you need this in a pattern, then you can use Condition:

f[x_] := ppp[x] /; x > 0
f[5]
f[-6]


OK, here is a different version of quick sort (I found on my disk - I am not sure how the author is...)

ClearAll[quickSort1]
quickSort1[lst0_] :=