Skip to main content
1 of 4
Ezareth
  • 389
  • 1
  • 10

Why If does not work instead of /; when searching condition in a list

I am trying to define a commutator. The way I am working this out is the following. I have a list with integers. I want to put the biggest on the right commuting them. For example, if I have a list like {1,1,1,-4,-3}, then I want to commute the three 1's to the right of the negative number.

I have a recursive function that does the job but I have issues with it since I overcharged this function's definition and the way Mathematica interprets it is not totally clear to me. Therefore, I want to go with an If,else condition. The first recursive function I have is the following

C3[a___, h2_, {n1___, m_, n_, n4___}, b___] := C3[a, h2, {n1, n, m, n4}, b] + (m - n) C3[a, h2, {n1, n + m, n4}, b] + 
If[m + n == 0, c (m^3 - m)/12 C3[a, h2, {n1, n4}, b], 0] /; m > n;

Now I don't understand why an equivalent thing with an If does not work. If I use the function

C3p[a___, h2_, {n1___, m_, n_, n4___}, b___] := If[m > n,C3[a, h2, {n1, n, m, n4},  b] + (m - n) C3[a, h2, {n1, n + m, n4}, b] +  If[m + n == 0, c (m^3 - m)/12 C3[a, h2, {n1, n4}, b], 0], 0]

This function does not even produce an output and never enters the condition. I am also interested in the rapidity of execution since in the end, this function may be called a huge number of time. Any help would be much appreciated.

Ezareth
  • 389
  • 1
  • 10