Entries from October 2009 ↓

Why Lua’s 0 (zero) as a true value makes sense

When I started to learn about Lua, I found it a little odd that the numeric 0 (zero) value is treated as a true boolean value by the interpreter, which was different from all other programming languages I had worked with.

However, it turns out that it makes a lot of sense to have 0 as a true value, and that’s when you take short circuit expressions into account.

For those who don’t know, short-circuit expressions are used to conditionally select values without having to code that selection into an if-elseif-else structure. Short-circuit expressions are built using logical (and, or, not) operators. The and operator returns the value of the first false expression, or the last expression, in case all others were true. The or operator returns the value of the first true expression, or the last expression, in case all others were false. Here are a few examples in Lua:

Code:

word = 1 < 2 and "hello" or "world"
print(word)

Output:

hello

Code:

level = 10 > 9 and 2 or 4
print(level)

Output:

2

Now, consider the following Lua short-circuit expression:

bit = math.random() < 0.5 and 0 or 1

Running it many times will psuedo-randomly assign both 0 and 1 values to the bit variable:

Code:

for i=1,8 do
    bit = math.random() < 0.5 and 0 or 1
    print(bit)
end

Output:

0
1
1
1
0
0
1
0

Now, consider what happens when we try to do the same thing in Python 2.6:

Code:

import random
for i in range(8):
    bit = random.random() < 0.5 and 0 or 1
    print(bit)

Output:

1
1
1
1
1
1
1
1

The first operand of the and expression is always false, because 0 is considered to be a false boolean value by the Python interpreter.

Even though one could argue that it’s just a matter of knowing how to rewrite such logical expressions, I believe it’s good not having to worry about undesired behavior caused by 0’s in short-circuit expressions. It allows me to write expressions just as they come to my mind, which makes me more productive than if I had to re-arrange them.

Creationism vs. Evolutionism

As a Christian, I’m just growing sick of that absolutely unfruitful discussion. I can’t stand listening to what theologians have to say, nor can I stand what atheists have to say. It’s all a huge load of garbage.

Here are my takes on the issue:

  1. People should only talk about what really concerns them. For me, a theologian who tries to force his faith into scientific reasoning is just as mistaken as the atheist who attempts to prove his unbelief using materialist arguments.
  2. Following the same line of reasoning from the item above, scientific books aren’t religious texts, nor are religious texts scientific books. As Francis Collins points out in The Language of God, scientific books describe the natural world, while religious books (for me, not bookS, only the Christian Bible) talk about the spiritual world.
  3. The whole thing doesn’t make a difference at all for me. The Book of Genesis presents an entirely spiritual issue (the fall of mankind). Whether things happened literally as described there, or if God used evolution as the mechanism for creation, is something that I don’t care much about. I find both alternatives equally admirable in my Creator.
  4. If people want to talk about science, they should talk about science. And if the want to talk about faith, they should talk about faith. Don’t try to mix them. Personally, I believe there is perfect agreement between them, but that agreement doesn’t seem to have been found yet. And we should probably not try to find it: up until now, it has proven to be quite an unhealthy thing.

Maybe I have some more points in the back of my mind, but I think the three above are the main ones. Those are personal opinions. I am NOT trying to convince anyone here. The only thing I would like to ask people for is:

Please stop talking about what you don’t know (or understand)!

And that goes to people in both sides of the issue.

A word for my fellow brothers in Christ: concentrate on what really matters, which is the Gospel of our Lord Jesus. It’s about love, forgiveness, wisdom, tolerance, respect, dignity, honor, and a bunch of other really good things. If you ever happen to discover if the universe was created in literal 7 days or if God decided to take billions of years to create will very likely not add a single bit to the kind and loving character you were taught to pursue.