Convert Infix expression to Postfix - stack

Convert the following infix expression to postfix:
True Or False And Not False Or False [Comptt 2005]
According to me, The answer should be:
True false false not and or false or
Considering the precedence order Not>and>or
((true or (false and (not false))) or false)
But according to the book, the answer is:
True False Or False Not and False or
They did it by: (((True or False)And (Not False))Or False)

Your answer is right.One clear mistake in the book answer is that precendence of conditional or is not greater than conditional and therefore the parenthesized expression would be:
((True Or (False and (NOTFalse)))Or False).

Related

Precedence of custom operators in F#

I'm trying to create operators similar to (&&) and (||), and with the same precedence.
My code is:
let (&&.) = (&&)
let (||.) = (||)
printf $"{true ||. true &&. false} = {true || true && false}"
This prints "False = True".
As I understand it, in F# the precedence of custom operators is defined by their first characters.
Why do I get this output, and how do I fix it?
I think this is working as intended. According to this table && has a higher precedence than ||, but &&. and ||. have the same precedence (as instances of &op and |op).
To fix it, you could choose an operator with higher precedence for your custom "and" function, like this:
let (^^.) = (&&)
let (||.) = (||)
printf $"{true ||. true ^^. false} = {true || true && false}" // "true = true"

What is this Agda error?

I'm trying out Agda for the first time and I've defined the Bool data type and its basic functions like all the tutorials say:
data Bool : Set where
true : Bool
false : Bool
not : Bool -> Bool
not true = false
not false = true
etc...
When I try to load this it gets upset because "more than one matching type signature for left hand side not true" and it highlights "not true" in red. What am I doing wrong?
You need to indent the data constructors of Bool.

How to interpret this lua expression with multiple logical operations in one line? [duplicate]

I saw this code in a Lua Style Guide
print(x == "yes" and "YES!" or x)
Context:
local function test(x)
print(x == "yes" and "YES!" or x)
-- rather than if x == "yes" then print("YES!") else print(x) end
end
What exactly happens at " x == "yes" and "YES!" ?
Why does it print "YES!" or (x) not "true" or (x) ?
EDIT:
Is it like:
X == "yes" -- > true
true and (value) -- > value
print( (x == "yes") and value)
So checking x for the value "yes" results in true, adding true to a value gives the value and printing this process prints the value, right?
From the docs:
The operator and returns its first argument if it is false; otherwise, it returns its second argument.
Therefore, true and "YES!" evaluates to "YES!".
This scheme works because if the first argument is falsy, the whole expression will become falsy (the same as the first argument); otherwise it will become the same as the second argument, which iff that is truthy will make the whole expression truthy.

why use `not not` operation?

In the following Lua code:
function eq_event(op1, op2)
if op1 == op2 then
return true
end
local h = getequalhandler(op1, op2)
if h then
return not not h(op1, op2)
else
return false
end
end
why use not not before the return value? is it different with the raw return value? I also remember that in C, there also got chance to use the !! before some expression, does them the same?
not not will convert nil into false, and all other values except false to true. You may need to return a boolean value only when interacting with C.
Yes it's the same as !! in C. Only difference is that in C, 0 (and some other values I don't remember) is also falsy (that is, they'll return false when doubled not'd).

Inline conditions in Lua (a == b ? "yes" : "no")?

Is there anyway to use inline conditions in Lua?
Such as:
print("blah: " .. (a == true ? "blah" : "nahblah"))
Sure:
print("blah: " .. (a and "blah" or "nahblah"))
If the a and t or f doesn't work for you, you can always just create a function:
function ternary ( cond , T , F )
if cond then return T else return F end
end
print("blah: " .. ternary(a == true ,"blah" ,"nahblah"))
of course, then you have the draw back that T and F are always evaluated....
to get around that you need to provide functions to your ternary function, and that can get unwieldy:
function ternary ( cond , T , F , ...)
if cond then return T(...) else return F(...) end
end
print("blah: " .. ternary(a == true ,function() return "blah" end ,function() return "nahblah" end))
You can usually do:
condition and ifTrue or ifFalse
but this isn't necessarily the best way to do it. The major reason why is because if ifTrue is a falsy value (some of the time), ifFalse will evaluate even if condition is a truthy value. One way to do it simply without much extra work is:
(condition and {ifTrue} or {ifFalse})[1]
which has the advantage of not only being an expression and not being subject to the problem of ifTrue being falsy which means it can handle all cases, but also has the advantage of short-circuiting (not evaluating the other expression). No need for extra functions or messing with complex aspects of Lua.
Although this question is fairly very old, I thought it would be fair to suggest another alternative that syntactically appears very similar to that of the ternary operator.
Add this:
function register(...)
local args = {...}
for i = 1, select('#', ...) do
debug.setmetatable(args[i], {
__call = function(condition, valueOnTrue, valueOnFalse)
if condition then
return valueOnTrue
else
return valueOnFalse
end
end
})
end
end
-- Register the required types (nil, boolean, number, string)
register(nil, true, 0, '')
And then use it like this:
print((true) (false, true)) -- Prints 'false'
print((false) (false, true)) -- Prints 'true'
print((nil) (true, false)) -- Prints 'false'
print((0) (true, false)) -- Prints 'true'
print(('') (true, false)) -- Prints 'true'
Note: For tables, however, you cannot use them directly with the above method. This is because each and every table has it's own independent metatable and Lua does not allow you to modify all tables at once.
In our case, an easy solution would be to convert the table into a boolean using the not not trick:
print((not not {}) (true, false)) -- Prints 'true'
You could just write the if statement in one line, it is no shorthand, inline or ternary operator stuff tho.
if (dummy) then
print("dummy is true")
end
is equal too
if (dummy) then print("dummy is true") end
Have fun
:D
local n = 12
do
local x = (n>15)
and print(">15")
or n>13
and print(">13")
or n>5
and print(">5")
end

Resources