12

So, in weakly-typed C, 0 is evaluated as false in a boolean context (like a conditional expression) and everything else is truthy. Modern dynamic languages seem to split on whether or not zero is falsy: Python, PHP, and Javascript say yes while Ruby, Lua, Clojure, and Racket say no.

Question is, why? I googled for my question title and found this thread on hacker news which had interesting stuff but not the answer to my question. Substituting 'lisp' for Clojure in my search yielded no historical reference.

So to be more precise in formulating my question: is there an actual technical advantage to zero evaluating as true in a conditional expression, or did it just become a quasi-standard in dynamic languages (that weren't as influenced by C) courtesy of the early lisps? Its definitely a bit odd to a programmer used to C/Python/Javascript.

3 Answers 3

25

The main reason is because many Clojure functions want to be able to easily tell "nothing" apart from "something". nil is the absence of a value, but the number 0 has a real value. An empty list is also a real thing, it's a sequence that could (for example) be conjed. (Though since Clojure allows you to (conj nil 1) that's perhaps not the best example.)

But the key distinction is that 0 is not "nothing", it's "something". Only "nothing" (and Boolean false) are going to be falsey in Clojure. It's a different way of thinking about falseness, and it can be jarring when you're coming from a Python background (as I did too), but I've actually found it more useful than Python's falsey mechanics.

Sign up to request clarification or add additional context in comments.

2 Comments

That's a sensible explanation. I guess I'm still caught in the mental mapping of 0 and 1 to transistor states and binary truth tables.
I'd prefer that nothing would give nothing in boolean logic, same as NULL in SQL.
4

One common idiom that pierce many languages is that language need something to represent nothing. C is one of the low-level of high-level languages that works with pointers directly, so it seems logical to make as nothing the impossible address, which is 0. So 0 pointer means nothing. And address can be considered (and actually it is) as a number. In Java there is null and in Clojure there is nil to represent nothing.

Question from the other side is: Should we consider something and nothing as bool values? In C, originally, there was no bool type, so they had to consider nothing as false (actually maybe in reverse: there is something and nothing so we don't need boolean type). In Clojure there is a boolean type and it differs from nil, but the simple rule of thumb works like this: "Nothing and false" coerce to FALSE and other to TRUE. So 0 is TRUE in Clojure. This was done, mostly to lisp original ideology, and, perhaps because time show that it is common case when you don't want to work with nothing, so you just check it.

1 Comment

rmunn beat you to it, nevertheless +1 for the bit about C pointers and the 0 address.
2

Well, the answer is both yes and no, as far as I see it, the only advantage to having zero evaluated as a non-false value is to reserve that "special" trait to Null (or None).

In Python, for instance, not only 0 and None are false - but also an empty list ([]) and an empty string (''). This allows a very readable code (most of the time), but has the down-side of having to do some specific testing if you want to override any of this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.