This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Dog's profile picture

Ternary question (are these the same?)

Started by Dog, 12 April 2014 - 04:20 AM
Dog #1
Posted 12 April 2014 - 06:20 AM
I've been trying to expand my understanding of (get a grasp on) ternary in Lua, so I've been reading (and re-reading…) a link provided by Bomb Bloke.

Starting with this…

if secondPoll == true then
  secondPoll = false
else
  secondPoll = true
end

I arrived at this…

if secondPoll == true then false else true end

then this…

if secondPoll then false else true end

and ultimately this…

secondPoll == true and false or true

Before I start assuming I'm getting it…are all four of these actually the same?
theoriginalbit #2
Posted 12 April 2014 - 06:49 AM
Well you could test this yourself with some simple prints. but the answer is no, these are not the same and it is due to how Lua works. It will resolved the 'false' (it will also resolve 'nil') you have in the 'if-true' condition and then move on to the 'if-false'.

To write a statement that matches your if statement you must do

not secondPoll and true or false

however I really must say, if you're validating a boolean, why not just do this

secondPoll = secondPoll == true

or if you're inverting the boolean, why not

secondPoll = not secondPoll
Dog #3
Posted 12 April 2014 - 08:12 AM
Thanks for the reply, bit :)/>

…these are not the same and it is due to how Lua works. It will resolved the 'false' (it will also resolve 'nil') you have in the 'if-true' condition and then move on to the 'if-false'.
I've re-read this several times and I'm embarrassed to say I don't understand this - probably because I clearly don't understand what's happening with this syntax (although I'm trying).

To write a statement that matches your if statement you must do

not secondPoll and true or false
After going over this one several times I think I understand the logic behind it. If secondPoll is true, it'll evaluate false and be set false, otherwise it'll evaluate true (if false) and be set to true…right?

however I really must say, if you're validating a boolean, why not just do this

secondPoll = secondPoll == true
This boggles me - would you be willing to expand on what's happening here?

or if you're inverting the boolean, why not

secondPoll = not secondPoll
That makes perfect sense. This is one of those 'why didn't I see that?' moments. So obvious and simple.

When I originally wrote the example I changed the variable values to boolean just before posting in order to simplify the example…or so I thought. I'm already seeing that "A" and "B" instead of true or false means approaching this differently. Replacing true and false in my first example, I'm guessing this might work…

secondPoll == "A" and "B" or "C"

same as?

if secondPoll == "A" then
  secondPoll = "B"
else
  secondPoll = "C"
end
But I have no idea if this will work…

secondPoll == "A" and "B" or "A"

same as? (I'm thinking it's not)

if secondPoll == "A" then
  secondPoll = "B"
else
  secondPoll = "A"
end

I thought I was starting to get this, but now I'm thinking I need to re-read everything again and let it simmer for awhile. This is definitely outside of my Lua comfort zone, which is what I'm going for - but I'm wondering if I'm getting too far ahead of myself. My old dog brain just doesn't learn like it used to…very frustrating.
Edited on 12 April 2014 - 06:18 AM
theoriginalbit #4
Posted 12 April 2014 - 08:40 AM
Okay so in Lua false and nil will evaluate to false, any other value will evaluate to true

the logic behind a normal ternary is [condition] ? [if true] : [if false] Lua implements the ternary operator differently however, by use of boolean logic and and or.

Boolean AND truth table

true and true = true
true and false = false
false and true = false
false and false  = false

Boolean OR truth table

true or true = true
true or false = true
false or true = true
false or false = false

Boolean NOT truth table

not true = false
not false = true

now Lua makes use of Short-Circuit Evaluation (I believe they call it Short-Circuit Logic) where when resolving a conditional if the operators used will no longer effect the output, it will stop evaluating and skip the remainder of the conditional. So that is to say, lets assume we have the following conditional

if not underAttack or (health >= 0 and canMove) then
  print("The ship is fine")
end
now lets assume underAttack is `false`, not false is `true`, now the next thing it would evaluate makes use of an OR taking a look back at the truth table for OR you'll notice that combining anything with `true` will result in `true`, therefore Lua will stop evaluating the conditional and perform the contents of the if statement. Now you might ask, but that is an if statement, what does this have to do with the ternary? well the concept is the same, take the following example (using booleans here for ease of reading)

secondPoll == "A" and true or false
now lets assume secondPoll is set to "A", therefore secondPoll == "A" resolves to `true`, the ternary now reads

true and true or false
now looking at our truth tables, we perform the AND as there are no brackets dictating order, that makes the ternary

true or false
looking at the OR truth table we now know this will resolve to `true`. now lets invert so that false is our 'if-true' value

secondPoll == "A" and false or true
again lets assume secondPoll is set to A, the ternary now reads

true and false or true
now in AND the truth table anything with `false` is `false`, making it read

false or true
and in the OR true table, anything with `true` is `true` meaning this will also resolve to true! So now that the easier to envision example is over, now lets use something with 'real' variables.

secondPoll == "A" and "B" or "C"
now again assuming secondPoll is set to A that will resolve to `true`, making it read

true and "B" or "C"
now lets evaluate the AND… `true` AND "B" (don't forget as I said Lua evaluates any non-nil value as `true`) meaning now we have

"B" or "C"
using the OR truth table you'll see that anything and `true` (remember "B" is `true`) is `true` so the conditional stops there and the result is "B"

I hope you followed along fine in this example, however if there's any parts you wish me to explain again, I can do so.

now as for your question about

var = var == true
this is simply just an equality check, equality checks return true or false, therefore in a plain English sentence this says "make var equal whether var is already equal to true" meaning if var is `true` it will remain `true`, if it is anything else it will become `false`.

this is why doing this

if turtle.dig() == true then
end
is pointless, dig returns true, there is no need to then have the following conditional

if true == true then
end
as `true` will always be equal to `true`, meaning it is a redundant check.

equally as pointless is the following function

local function dig()
  if turtle.dig() then
	return true
  else
	return false
  end
end
it could simply be

local function dig()
  return turtle.dig()
end

I hope all of this makes sense and you've learnt some things about, well, programming in general (truth tables perform the same way in all languages!)

EDIT: in the sense of the question you edited in, I hope you're able to work out the answer from my above explanation that the answer is yes, they are the same, just in the sense of using the booleans like you did in the example they're not.

EDIT 2: oh i should also mention. lets assume we have a variable, and when its nil give it a default value, we can do it like this

var = var or "hello"
so obviously if var is `false` or `nil` in this case, it will then become "hello" however if it is anything other value it will remain as the value. which you should also understand from learning the explanations above.
Edited on 12 April 2014 - 06:52 AM
Dog #5
Posted 12 April 2014 - 09:08 AM
Wow, thank you. I've read through your post once and I get most of it (I think), although some is still a bit mysterious to me (probably because it's 1AM here). I need to sleep on this. With apologies, can we take this up tomorrow?
theoriginalbit #6
Posted 12 April 2014 - 09:15 AM
definitely. have a good sleep.