Short-circuit evaluation: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
Simple9371 (talk | contribs) m (→{{header|Batch File}}: added 'on if command') |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(9 intermediate revisions by 8 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|
==={{header|BaCon}}===
BaCon supports short-circuit evaluation.
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}}==
Line 1,184 ⟶ 1,262:
<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>
Line 1,245 ⟶ 1,318:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Func<bool, bool> a = (bool x){ console.writeLine
Func<bool, bool> b = (bool x){ console.writeLine
const bool[] boolValues = new bool[]{ false, true };
Line 1,257 ⟶ 1,330:
public program()
{
boolValues.forEach::(bool i)
{
boolValues.forEach::(bool j)
{
console.printLine(i," and ",j," = ",a(i) && b(j));
Line 1,693 ⟶ 1,766:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Short-circuit_evaluation}}
'''Solution'''
[[File:Fōrmulæ - Short-circuit evaluation 01.png]]
[[File:Fōrmulæ - Short-circuit evaluation 02.png]]
=={{header|Go}}==
Line 1,916 ⟶ 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 4,973 ⟶ 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="
System.print(" a called")
return bool
|