Short-circuit evaluation: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(16 intermediate revisions by 11 users not shown)
Line 506:
{{true, false, false, false}, {true, true, true, false}}
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">a: function [v][
print ["called function A with:" v]
v
]
 
b: function [v][
print ["called function B with:" v]
v
]
 
loop @[true false] 'i ->
loop @[true false] 'j ->
print ["\tThe result of A(i) AND B(j) is:" and? -> a i -> b j]
 
print ""
 
loop @[true false] 'i ->
loop @[true false] 'j ->
print ["\tThe result of A(i) OR B(j) is:" or? -> a i -> b j]</syntaxhighlight>
 
{{out}}
 
<pre>called function A with: true
called function B with: true
The result of A(i) AND B(j) is: true
called function A with: true
called function B with: false
The result of A(i) AND B(j) is: false
called function A with: false
The result of A(i) AND B(j) is: false
called function A with: false
The result of A(i) AND B(j) is: false
 
called function A with: true
The result of A(i) OR B(j) is: true
called function A with: true
The result of A(i) OR B(j) is: true
called function A with: false
called function B with: true
The result of A(i) OR B(j) is: true
called function A with: false
called function B with: false
The result of A(i) OR B(j) is: false</pre>
 
=={{header|AutoHotkey}}==
Line 583 ⟶ 629:
Return</syntaxhighlight>
 
=={{header|BaConBASIC}}==
==={{header|BaCon}}===
BaCon supports short-circuit evaluation.
 
Line 632 ⟶ 679:
=={{header|Batch File}}==
{{trans|Liberty BASIC}}
<syntaxhighlight lang="dos">%=== Batch Files have no booleans. on if command, let alone short-circuit evaluation ===%
%=== I will instead use 1 as true and 0 as false. ===%
 
Line 1,179 ⟶ 1,226:
<syntaxhighlight lang="e">def x := a(i) && (def funky := b(j))</syntaxhighlight>
The choice we make is that <code>funky</code> is ordinary if the right-side expression was evaluated, and otherwise is <em>ruined</em>; attempts to access the variable give an error.
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
func a x .
print "->a: " & x
return x
.
func b x .
print "->b: " & x
return x
.
print "1 and 1"
if a 1 = 1 and b 1 = 1
print "-> true"
.
print ""
print "1 or 1"
if a 1 = 1 or b 1 = 1
print "-> true"
.
print ""
print "0 and 1"
if a 0 = 1 and b 1 = 1
print "-> true"
.
print ""
print "0 or 1"
if a 0 = 1 or b 1 = 1
print "-> true"
.
</syntaxhighlight>
 
=={{header|Ecstasy}}==
Similar to Java, Ecstasy uses the <span style="background-color: #e5e4e2">&nbsp;&amp;&amp;&nbsp;</tt></span> and <span style="background-color: #e5e4e2"><tt>&nbsp;||&nbsp;</tt></span> operators for short-circuiting logic, and <span style="background-color: #e5e4e2"><tt>&nbsp;&amp;&nbsp;</tt></span> and <span style="background-color: #e5e4e2"><tt>&nbsp;|&nbsp;</tt></span> are the normal (non-short-circuiting) forms.
 
<syntaxhighlight lang="java">
module test {
@Inject Console console;
 
static Boolean show(String name, Boolean value) {
console.print($"{name}()={value}");
return value;
}
 
void run() {
val a = show("a", _);
val b = show("b", _);
 
for (Boolean v1 : False..True) {
for (Boolean v2 : False..True) {
console.print($"a({v1}) && b({v2}) == {a(v1) && b(v2)}");
console.print();
console.print($"a({v1}) || b({v2}) == {a(v1) || b(v2)}");
console.print();
}
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
a()=False
a(False) && b(False) == False
 
a()=False
b()=False
a(False) || b(False) == False
 
a()=False
a(False) && b(True) == False
 
a()=False
b()=True
a(False) || b(True) == True
 
a()=True
b()=False
a(True) && b(False) == False
 
a()=True
a(True) || b(False) == True
 
a()=True
b()=True
a(True) && b(True) == True
 
a()=True
a(True) || b(True) == True
</pre>
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Func<bool, bool> a = (bool x){ console.writeLine:("a"); ^ x };
Func<bool, bool> b = (bool x){ console.writeLine:("b"); ^ x };
const bool[] boolValues = new bool[]{ false, true };
Line 1,193 ⟶ 1,330:
public program()
{
boolValues.forEach::(bool i)
{
boolValues.forEach::(bool j)
{
console.printLine(i," and ",j," = ",a(i) && b(j));
Line 1,629 ⟶ 1,766:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Short-circuit_evaluation}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Short-circuit evaluation 01.png]]
In '''[https://formulae.org/?example=Short-circuit_evaluation this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Short-circuit evaluation 02.png]]
 
=={{header|Go}}==
Line 1,852 ⟶ 1,991:
Shortcircuit.icn: 16 | true returned &null
i,j := procedure true, procedure true</pre>
 
=={{header|Insitux}}==
{{trans|Clojure}}
<syntaxhighlight lang="insitux">
(let a (fn (print-str "a ") %)
b (fn (print-str "b ") %)
f (pad-right " " 6))
 
(for i [true false] j [true false]
(print-str (f i) "OR " (f j) " = ")
(print (or (a i) (b j)))
(print-str (f i) "AND " (f j) " = ")
(print (and (a i) (b j))))
</syntaxhighlight>
{{out}}
<pre>
true OR true = a true
true AND true = a b true
true OR false = a true
true AND false = a b false
false OR true = a b true
false AND true = a false
false OR false = a b false
false AND false = a false
</pre>
 
=={{header|Io}}==
Line 2,324 ⟶ 2,488:
x = a(i) and b(i); print ""
y = a(i) or b(i)</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Short_circuit_evaluation {
function a(a as boolean) {
=a
doc$<=format$(" Called function a({0}) -> {0}", a)+{
}
}
function b(b as boolean) {
=b
doc$<=format$(" Called function b({0}) -> {0}", b)+{
}
}
boolean T=true, F, iv, jv
variant L=(F, T), i, j
i=each(L)
global doc$ : document doc$
while i
j=each(L)
while j
(iv, jv)=(array(i), array(j))
doc$<=format$("Calculating x = a({0}) and b({1}) -> {2}", iv, jv, iv and jv)+{
}
x=if(a(iv)->b(jv), F)
doc$<=format$("x={0}", x)+{
}+ format$("Calculating y = a({0}) or b({1}) -> {2}", iv, jv, iv or jv)+{
}
y=if(a(iv)->T, b(jv))
doc$<=format$("y={0}", y)+{
}
end while
end while
clipboard doc$
report doc$
}
Short_circuit_evaluation
</syntaxhighlight>
{{out}}
<pre>
Calculating x = a(False) and b(False) -> False
Called function a(False) -> False
x=False
Calculating y = a(False) or b(False) -> False
Called function a(False) -> False
Called function b(False) -> False
y=False
 
Calculating x = a(False) and b(True) -> False
Called function a(False) -> False
x=False
Calculating y = a(False) or b(True) -> True
Called function a(False) -> False
Called function b(True) -> True
y=True
 
Calculating x = a(True) and b(False) -> False
Called function a(True) -> True
Called function b(False) -> False
x=False
Calculating y = a(True) or b(False) -> True
Called function a(True) -> True
y=True
 
Calculating x = a(True) and b(True) -> True
Called function a(True) -> True
Called function b(True) -> True
x=True
Calculating y = a(True) or b(True) -> True
Called function a(True) -> True
y=True
 
</pre>
 
=={{header|Maple}}==
Line 3,521 ⟶ 3,759:
Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True
# Called function a(True) -> True</syntaxhighlight>
 
=={{header|Quackery}}==
 
Quackery does not include short-circuit evaluation, but it can be added by use of meta-control flow words (words wrapped in reverse brackets such as <code>]done[</code>.) For details see: [https://github.com/GordonCharlton/Quackery/blob/main/The%20Book%20of%20Quackery.pdf The Book of Quackery]
 
The short-circuit evaluation words <code>SC-and</code> and <code>SC-or</code> defined here are used thus: <code>[ a 1 SC-and b ]</code> and <code>[ a 1 SC-or b ]</code>. These presumes that the arguments to <code>a</code> and <code>b</code> are on the stack in the order <code>j i</code>.
 
The <code>1</code> preceding the word is required to indicate the number of arguments that <code>b</code> would consume from the stack if it were evaluated.
 
Extending the task to three functions, the third, <code>c</code> also consuming one argument <code>k</code> and returning a boolean, present on the stack underneath <code>j</code> and <code>i</code> would lead to the code <code>[ a 2 SC-and b 1 SC-and c ]</code> and <code>[ a 2 SC-or b 1 SC-or c ]</code>, where the <code>2</code> is the sum of the number of arguments consumed by <code>b</code> and <code>c</code>, and the <code>1</code> is the number of arguments consumed by <code>c</code>.
 
<code>SC-and</code> and <code>SC-or</code> can both be used in a single short-circuit evaluation nest with three or more functions. Evaluation is strictly left to right.
 
Quackery does not have variables, so no assignment is shown here. Words (functions) leave their results on the stack. If desired results can be moved to ancillary stacks, which include standing-in for variables amongst their functionality.
 
<syntaxhighlight lang="Quackery">
[ say "evaluating "
]this[ echo cr ] is ident ( --> )
 
[ iff say "true"
else say "false" ] is echobool ( b --> )
 
[ swap iff drop done
times drop
false ]done[ ] is SC-and ( b n --> )
 
[ swap not iff drop done
times drop
true ]done[ ] is SC-or ( b n --> )
 
[ ident
2 times not ] is a ( b --> b )
 
[ ident
4 times not ] is b ( b --> b )
 
[ say "i = "
dup echobool
say " AND j = "
dup echobool
cr
[ a 1 SC-and b ]
say "result is "
echobool cr cr ] is AND-demo ( --> )
 
[ say "i = "
dup echobool
say " OR j = "
dup echobool
cr
[ a 1 SC-or b ]
say "result is "
echobool
cr cr ] is OR-demo ( --> )
 
true true AND-demo
true false AND-demo
false true AND-demo
false false AND-demo
cr
true true OR-demo
true false OR-demo
false true OR-demo
false false OR-demo</syntaxhighlight>
 
{{out}}
 
<pre>i = true AND j = true
evaluating a
evaluating b
result is true
 
i = false AND j = false
evaluating a
result is false
 
i = true AND j = true
evaluating a
evaluating b
result is false
 
i = false AND j = false
evaluating a
result is false
 
 
i = true OR j = true
evaluating a
result is true
 
i = false OR j = false
evaluating a
evaluating b
result is true
 
i = true OR j = true
evaluating a
result is true
 
i = false OR j = false
evaluating a
evaluating b
result is false</pre>
 
 
=={{header|R}}==
Line 4,731 ⟶ 5,073:
T F OR T a()
T T OR T a()
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
fn main() {
test_me(false, false)
test_me(false, true)
test_me(true, false)
test_me(true, true)
}
 
fn a(v bool) bool {
print("a")
return v
}
 
fn b(v bool) bool {
print("b")
return v
}
 
fn test_me(i bool, j bool) {
println("Testing a(${i}) && b(${j})")
print("Trace: ")
println("\nResult: ${a(i) && b(j)}")
 
println("Testing a(${i})} || b(${j})")
print("Trace: ")
println("\nResult: ${a(i) || b(j)}")
println("")
}
</syntaxhighlight>
 
{{out}}
<pre>
Testing a(false) && b(false)
Trace: a
Result: false
Testing a(false)} || b(false)
Trace: ab
Result: false
 
Testing a(false) && b(true)
Trace: a
Result: false
Testing a(false)} || b(true)
Trace: ab
Result: true
 
Testing a(true) && b(false)
Trace: ab
Result: false
Testing a(true)} || b(false)
Trace: a
Result: true
 
Testing a(true) && b(true)
Trace: ab
Result: true
Testing a(true)} || b(true)
Trace: a
Result: true
</pre>
 
=={{header|Wren}}==
Wren has the '''&&''' and '''||''' short-circuiting operators found in many C family languages.
<syntaxhighlight lang="ecmascriptwren">var a = Fn.new { |bool|
System.print(" a called")
return bool
9,485

edits