The length of an empty list in math

I really don't know why the code output:

State_Values = List[]; Print[Length[{}]] Print[Length[State_Values]]; 

:

 0 2 

It is impossible to offer any reason. It may be very stupid, but I do not see. Thanks.

+4
source share
1 answer

Not stupid, but you made a subtle mistake. An underscore in State_Values turns it into a Pattern , not a List . You can find this using the Head[] function.

 stateValues = List[]; Length[stateValues] Length[{}] Out[11]= 0 Out[12]= 0 

As you can see, this is correct and expected. Enter the underscore and it all breaks down:

 state_Values = List[]; Length[state_Values] Head[state_Values] Head[stateValues] Out[16]= 2 Out[17]= Pattern Out[18]= List 

It is much easier to see if you are using the graphical version of Mathematica, as it highlights Pattern variables differently.

+10
source

All Articles