Message Boards Message Boards

0
|
2271 Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Missing[] vs Null

Posted 1 year ago

Hello,
I see there are Missing[] and Null in Mathematica, but what are their differences?
Thanks.

POSTED BY: Zhenyu Zeng
6 Replies
Posted 1 year ago

Okay. I know more from your reply. Thanks a lot!

POSTED BY: Zhenyu Zeng
Posted 1 year ago
  1. "Plain old" is an idiom. I wrote "plain 'ol" but I should have written "plain ol'". An apostrophe can mean that letters are missing. Since that idiom is often spoken without pronouncing the "d", I spelled it without the "d". "Plain old" means ordinary. I used it here to emphasize that there is nothing special about Null that would set it apart from other symbols in terms of expression evaluation. "It's a plain ol' symbol" means "it's an ordinary symbol".

  2. When I used "a;" I meant literally "a;". Open Mathematica and type literally those two characters, the letter a followed by a semi-colon. Without any context, I expected you to understand that "a" was a symbol. It could have been any symbol: "abc;" or "undefinedSymbol;". Indeed, for my example it could have been any expression whatever: "1+2+x^2;" or "Sqrt[5];". All of these would result in Null.

That second point might require explanation. In Mathematica, anything that's not an atomic thing is an expression with a head. Mathematica offers special syntax forms to make things easier to type and easier to read, but those forms get immediately transformed into their full expression form. When you evaluate "{1,2,3}" that gets parsed immediately into "List[1,2,3]". The semi-colon is one of these special forms. The semicolon is infix notation for CompoundExpression. So, if you evaluate "a=1;b=2", you're really evaluating "CompoundExpression[a=1,b=2]". If you evaluate "a=1;b=2;", you're really evaluting "CompoundExpression[a=1,b=2,Null]"

POSTED BY: Eric Rimbey
Posted 1 year ago

Null is kind of special. On the one hand, it's a plain 'ol symbol. You can use it the same way you could use any other symbol. And symbols are just expressions, so you can use Null wherever you can use an expression (it's not guaranteed to produce meaningful results, of course):

5 Null
Null[]
ConstantArray[Null, 5]

On the other hand, Null is automatically inserted if the user has left a part of an expression empty where the syntax rules require an expression to be:

{,} becomes {Null,Null}

a; evaluates to Null

You might not see Null explicitly in all of these cases, because the display form for a raw Null shows nothing.

Null has no semantics associated to it. Of course, you are free to adopt your own semantics for it in your own programs, but by default it's just an inert token that the system can use to make sure an expression is well-formed.

On the other hand, Missing presumes some semantics. The system will generate a Missing expression, for example, if you try to access an association with a missing key, or if you try to access an entity or property that is missing. You can supply an argument to Missing to further clarify the semantics. For example Missing["Unknown"] would be used differently than Missing["Nonexistent"]. Several built in functions have rules for handling Missing, typically by just ignoring it. There is also a nice helper function DeleteMissing.

POSTED BY: Eric Rimbey
Posted 1 year ago

Hello, Thanks for your help. I have several questions after reading your reply:

  1. On the one hand, it's a plain 'ol symbol. What's `ol symbol?
  2. a; evaluates to Null. What is a and why it evaluates to Null?

Thanks.

POSTED BY: Zhenyu Zeng

About question 2: Execute this:

FullForm[Hold[a;]]

You will see that the full form contains a Null (this is how the input syntax of WL is defined):

HoldForm[CompoundExpression[a,Null]]

And CompoundExpression returns the last expression (evaluated, but Null evaluates to Null).

Null has been around since the beginning. When I was learning Mathematica and coming from the C & Pascal programming languages, I used to think of Null as what a procedure returned (void in C) as opposed to an "actual" value returned by a function (which has to have a type in C). Of course, every WL function returns something. But if you execute a command that returns Null, you get, as Eric already mentioned, no output in the notebook. That seemed like a "procedure" to me.

Missing[] was introduced as Mathematica was expanded to handle data and data sets, including curated data like CountryData[]. Since Missing[] may have arguments, it allows for more robust handling of data, which has grown in importance since Mathematica began. (By "data sets" I don't mean Dataset, which was a big step in the evolution of how data is handled and came a while after Missing[] was introduced.)

POSTED BY: Michael Rogers
Posted 1 year ago

Thanks a lot. Your answer makes me understand more deeply about Mathematica.

POSTED BY: Zhenyu Zeng
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract