Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length of empty List in mathematica

I really don`t know why the output of the code:

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

is :

0 
2

Can't suggest any reason. Maybe it is sth very stupid, but I can't see. Thank you.

like image 511
sdd Avatar asked Jun 29 '12 16:06

sdd


1 Answers

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

stateValues = List[];
Length[stateValues]
Length[{}]

Out[11]= 0

Out[12]= 0

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

state_Values = List[];
Length[state_Values]
Head[state_Values]
Head[stateValues]

Out[16]= 2

Out[17]= Pattern

Out[18]= List

It's much easier to see if you're using the GUI version of Mathematica since it highlights Pattern variables differently.

like image 98
Tim Avatar answered Dec 23 '22 15:12

Tim