This question already has answers here:
Is Perl optimized to skip remaining logic operands if the answer is already decided?
(6 answers)
Closed 7 years ago.
if(expression1 or expression 2)
{
do something
}
If 'expression1' returns true, does the compiler starts to execute 'do something' or it evaluates the second expression too?
No, expression2 will not be evaluated if expression1 is true.
This is because or is short-circutted in perl: once the result of the entire expression is known, evaluation stops. Evaluation occurs from left to right.
Related
This question already has answers here:
What do curly braces mean in Verilog?
(2 answers)
Closed 3 years ago.
I am starting to learn SystemVerilog. I am stuck with a priority encoder and can't get this part :
priority if (encoder_in == {{14{1'bx}},1'b1,{1{1'b0}}})
this is actually a concatenation of 3 constants.
{ {14{1'bx}}, 1'b1, {1{1'b0}} }
1--^^^^^^^^^^
2--------------^^^^
3--------------------^^^^^^^^^
is a replication operator and it generates a 14-bits of 'x'.
is a one-bit 1
is a replication operator with a single repetition. I have no idea why it is used this way.
The following would be an equivalent expression:
{{14{1'bx}}, 1'b1, 1'b0}
or this
{{14{1'bx}}, 2'b10}
or this:
16'xxxxxxxxxxxxxx10
Next, priority is a system verilog modifier which could be applied to an if or a case operators. Read about unique end priority modifiers in system verilog.
This question already has answers here:
Are there good uses for non-short-circuiting logical (boolean) operators in Java/Scala?
(4 answers)
What is the difference between the | and || or operators?
(12 answers)
Why doesn't the bitwise & operator short-circuit?
(5 answers)
Closed 5 years ago.
I am trying to figure out the difference between & and && in Scala. I got this after searching
& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false
Can somebody explain the same with example as am not clear, what verifying both operands means here. Are we talking about just Booleans?
Both are logical AND operators in Scala. The first form, &, will evaluate both operands values, for example:
val first = false
val second = true
if (first & second) println("Hello!")
Will evaluate both first and second before exiting the if condition, although we know that once a false appears in a logical AND, that entire expression will already yield false.
This is what && is for, and what it does is short-circuit the evaluation, meaning you only ever evaluate firsts value and then exit the conditional.
You can use bitwise AND (&) to perform bitwise operations on integers, which is similar to most programming languages.
This question already has an answer here:
Test if array is inside a list in lisp
(1 answer)
Closed 7 years ago.
I have a problem with removing strings from a list of string I use this
(remove "lol" '("lol" "lol2" "lol")
but it returns the same list. What's the problem here?
You're running into the problem of trying to determine equality. I believe remove uses eql as its default equality tester. Unfortunately, two strings are not eql unless they're actually the same object.
Try:
`(remove "lol" '("lol" "lol2" "lol") :test #'equal)
Alternatively, if you know you will be testing strings, you could pass string= as your test function.
You should close the ')'
So write : (remove "lol" '("lol" "lol2" "lol"))
This question already has answers here:
What do all of Scala's symbolic operators mean?
(11 answers)
Closed 8 years ago.
What does :: mean here ?
listeners ::= listener
list = num :: list
Specially I don't understand the "::" operator.
Its the list cons operator. It creates a new list whose head is first argument and whose tail is contents of the second argument.
This question already has answers here:
Meaning of // operator in perl
(5 answers)
Closed 9 years ago.
I was searching in a lot of Perl books but I can't find an answer. I have this code, what I suppose it does is assign param's ticket to $ticket iff it exists if not, assign 0.
my $ticket = $params->{ticket} // 0;
// means defined-or. $ticket is assigned $params->{ticket} if it is defined, 0 otherwise.
Although it has no direct equivalent in C, Perl's // operator is related to its C-style or. In fact, it's exactly the same as ||, except that it tests the left hand side's definedness instead of its truth. Thus, EXPR1 // EXPR2 returns the value of EXPR1 if it's defined, otherwise, the value of EXPR2 is returned.
It was added in 5.10.
In the code above, $params->{ticket} can still have garbage in it, so make sure the value conforms to the expected pattern before using it.
Perl documentation says:
"EXPR1 // EXPR2 returns the value of EXPR1 if it's defined, otherwise, the value of EXPR2 is returned."
It's similar to a logic or, but testing definedness.