Erlang is the first language I come across to give true for nil > 0.
What is the story behind this decision?
Other languages seem to behave differently.
Python:
None > 0
# False
JavaScript:
null > 0
// false
Ruby:
nil > 0
NoMethodError: undefined method `>' for nil:NilClass
2条答案
按热度按时间66bbxpm51#
在Erlang中,任何术语都可以与任何其他术语进行比较。Erlang术语比较的顺序为:
因此nil〉0为true有关期限比较的更多信息
inn6fuwd2#
Yes, in Erlang,
nil > 0
evaluates to true. But when Erlang programmer talks about "nil", he means the empty list:[]
. If you writenil
in Erlang code, it's just an atom'nil'
, like'dog'
or'hello'
. And any atom is larger than any number because of the following built-in hierarchy:But it's very important to note that "nil" in this line refers to empty list
[]
, not to the atom'nil'
.As a side note: Evaluating
[] > 0
also returns true because any number is smaller than nil.And finally,
[] > nil
also returns true because nil ([]
) is larger than any atom ('nil'
).