Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Short-circuit-evaluation/00-META.yaml
Normal file
3
Task/Short-circuit-evaluation/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Short-circuit_evaluation
|
||||
note: Programming language concepts
|
||||
29
Task/Short-circuit-evaluation/00-TASK.txt
Normal file
29
Task/Short-circuit-evaluation/00-TASK.txt
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{{Control Structures}}
|
||||
|
||||
Assume functions <code>a</code> and <code>b</code> return boolean values, and further, the execution of function <code>b</code> takes considerable resources without side effects, <!--treating the printing as being for illustrative purposes only--> and is to be minimized.
|
||||
|
||||
If we needed to compute the conjunction (<code>and</code>):
|
||||
:::: <code> x = a() and b() </code>
|
||||
|
||||
Then it would be best to not compute the value of <code>b()</code> if the value of <code>a()</code> is computed as <code>false</code>, as the value of <code>x</code> can then only ever be <code> false</code>.
|
||||
|
||||
Similarly, if we needed to compute the disjunction (<code>or</code>):
|
||||
:::: <code> y = a() or b() </code>
|
||||
|
||||
Then it would be best to not compute the value of <code>b()</code> if the value of <code>a()</code> is computed as <code>true</code>, as the value of <code>y</code> can then only ever be <code>true</code>.
|
||||
|
||||
Some languages will stop further computation of boolean equations as soon as the result is known, so-called [[wp:Short-circuit evaluation|short-circuit evaluation]] of boolean expressions
|
||||
|
||||
|
||||
;Task:
|
||||
Create two functions named <code>a</code> and <code>b</code>, that take and return the same boolean value.
|
||||
|
||||
The functions should also print their name whenever they are called.
|
||||
|
||||
Calculate and assign the values of the following equations to a variable in such a way that function <code>b</code> is only called when necessary:
|
||||
:::: <code> x = a(i) and b(j) </code>
|
||||
:::: <code> y = a(i) or b(j) </code>
|
||||
|
||||
<br>If the language does not have short-circuit evaluation, this might be achieved with nested '''if''' statements.
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
F a(v)
|
||||
print(‘ ## Called function a(#.)’.format(v))
|
||||
R v
|
||||
|
||||
F b(v)
|
||||
print(‘ ## Called function b(#.)’.format(v))
|
||||
R v
|
||||
|
||||
L(i) (0B, 1B)
|
||||
L(j) (0B, 1B)
|
||||
print("\nCalculating: x = a(i) and b(j)")
|
||||
V x = a(i) & b(j)
|
||||
print(‘Calculating: y = a(i) or b(j)’)
|
||||
V y = a(i) | b(j)
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
;DEFINE 0 AS FALSE, $FF as true.
|
||||
False equ 0
|
||||
True equ 255
|
||||
Func_A:
|
||||
;input: accumulator = value to check. 0 = false, nonzero = true.
|
||||
;output: 0 if false, 255 if true. Also prints the truth value to the screen.
|
||||
;USAGE: LDA val JSR Func_A
|
||||
BEQ .falsehood
|
||||
load16 z_HL,BoolText_A_True ;lda #<BoolText_A_True sta z_L lda #>BoolText_A_True sta z_H
|
||||
jsr PrintString
|
||||
jsr NewLine
|
||||
LDA #True
|
||||
rts
|
||||
.falsehood:
|
||||
load16 z_HL,BoolText_A_False
|
||||
jsr PrintString
|
||||
jsr NewLine
|
||||
LDA #False
|
||||
rts
|
||||
|
||||
Func_B:
|
||||
;input: Y = value to check. 0 = false, nonzero = true.
|
||||
;output: 0 if false, 255 if true. Also prints the truth value to the screen.
|
||||
;USAGE: LDY val JSR Func_B
|
||||
TYA
|
||||
BEQ .falsehood ;return false
|
||||
load16 z_HL,BoolText_B_True
|
||||
jsr PrintString
|
||||
jsr NewLine
|
||||
LDA #True
|
||||
rts
|
||||
.falsehood:
|
||||
load16 z_HL,BoolText_B_False
|
||||
jsr PrintString
|
||||
jsr NewLine
|
||||
LDA #False
|
||||
rts
|
||||
|
||||
|
||||
Func_A_and_B:
|
||||
;input:
|
||||
; z_B = input for Func_A
|
||||
; z_C = input for Func_B
|
||||
;output:
|
||||
;0 if false, 255 if true
|
||||
LDA z_B
|
||||
jsr Func_A
|
||||
BEQ .falsehood
|
||||
LDY z_C
|
||||
jsr Func_B
|
||||
BEQ .falsehood
|
||||
;true
|
||||
load16 z_HL,BoolText_A_and_B_True
|
||||
jsr PrintString
|
||||
jsr NewLine
|
||||
LDA #True
|
||||
rts
|
||||
.falsehood:
|
||||
load16 z_HL,BoolText_A_and_B_False
|
||||
jsr PrintString
|
||||
jsr NewLine
|
||||
LDA #False
|
||||
rts
|
||||
|
||||
Func_A_or_B:
|
||||
;input:
|
||||
; z_B = input for Func_A
|
||||
; z_C = input for Func_B
|
||||
;output:
|
||||
;0 if false, 255 if true
|
||||
LDA z_B
|
||||
jsr Func_A
|
||||
BNE .truth
|
||||
LDY z_C
|
||||
jsr Func_B
|
||||
BNE .truth
|
||||
;false
|
||||
load16 z_HL,BoolText_A_or_B_False
|
||||
jsr PrintString
|
||||
LDA #False
|
||||
rts
|
||||
.truth:
|
||||
load16 z_HL,BoolText_A_or_B_True
|
||||
jsr PrintString
|
||||
LDA #True
|
||||
rts
|
||||
|
||||
|
||||
BoolText_A_True:
|
||||
db "A IS TRUE",0
|
||||
BoolText_A_False:
|
||||
db "A IS FALSE",0
|
||||
BoolText_B_True:
|
||||
db "B IS TRUE",0
|
||||
BoolText_B_False:
|
||||
db "B IS FALSE",0
|
||||
|
||||
BoolText_A_and_B_True:
|
||||
db "A AND B IS TRUE",0
|
||||
BoolText_A_and_B_False:
|
||||
db "A AND B IS FALSE",0
|
||||
BoolText_A_or_B_True:
|
||||
db "A OR B IS TRUE",0
|
||||
BoolText_A_or_B_False:
|
||||
db "A OR B IS FALSE",0
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
lda #True
|
||||
sta z_B
|
||||
lda #True
|
||||
sta z_C
|
||||
|
||||
jsr Func_A_and_B
|
||||
|
||||
jsr NewLine
|
||||
|
||||
jsr Func_A_or_B
|
||||
|
||||
jmp *
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
PRIO ORELSE = 2, ANDTHEN = 3; # user defined operators #
|
||||
OP ORELSE = (BOOL a, PROC BOOL b)BOOL: ( a | a | b ),
|
||||
ANDTHEN = (BOOL a, PROC BOOL b)BOOL: ( a | b | a );
|
||||
|
||||
# user defined Short-circuit_evaluation procedures #
|
||||
PROC or else = (BOOL a, PROC BOOL b)BOOL: ( a | a | b ),
|
||||
and then = (BOOL a, PROC BOOL b)BOOL: ( a | b | a );
|
||||
|
||||
test:(
|
||||
|
||||
PROC a = (BOOL a)BOOL: ( print(("a=",a,", ")); a),
|
||||
b = (BOOL b)BOOL: ( print(("b=",b,", ")); b);
|
||||
|
||||
CO
|
||||
# Valid for Algol 68 Rev0: using "user defined" operators #
|
||||
# Note: here BOOL is being automatically "procedured" to PROC BOOL #
|
||||
print(("T ORELSE F = ", a(TRUE) ORELSE b(FALSE), new line));
|
||||
print(("F ANDTHEN T = ", a(FALSE) ANDTHEN b(TRUE), new line));
|
||||
|
||||
print(("or else(T, F) = ", or else(a(TRUE), b(FALSE)), new line));
|
||||
print(("and then(F, T) = ", and then(a(FALSE), b(TRUE)), new line));
|
||||
END CO
|
||||
|
||||
# Valid for Algol68 Rev1: using "user defined" operators #
|
||||
# Note: BOOL must be manually "procedured" to PROC BOOL #
|
||||
print(("T ORELSE F = ", a(TRUE) ORELSE (BOOL:b(FALSE)), new line));
|
||||
print(("T ORELSE T = ", a(TRUE) ORELSE (BOOL:b(TRUE)), new line));
|
||||
|
||||
print(("F ANDTHEN F = ", a(FALSE) ANDTHEN (BOOL:b(FALSE)), new line));
|
||||
print(("F ANDTHEN T = ", a(FALSE) ANDTHEN (BOOL:b(TRUE)), new line));
|
||||
|
||||
print(("F ORELSE F = ", a(FALSE) ORELSE (BOOL:b(FALSE)), new line));
|
||||
print(("F ORELSE T = ", a(FALSE) ORELSE (BOOL:b(TRUE)), new line));
|
||||
|
||||
print(("T ANDTHEN F = ", a(TRUE) ANDTHEN (BOOL:b(FALSE)), new line));
|
||||
print(("T ANDTHEN T = ", a(TRUE) ANDTHEN (BOOL:b(TRUE)), new line))
|
||||
|
||||
)
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
test:(
|
||||
|
||||
PROC a = (BOOL a)BOOL: ( print(("a=",a,", ")); a),
|
||||
b = (BOOL b)BOOL: ( print(("b=",b,", ")); b);
|
||||
|
||||
# Valid for Algol 68G and 68RS using non standard operators #
|
||||
print(("T OREL F = ", a(TRUE) OREL b(FALSE), new line));
|
||||
print(("T OREL T = ", a(TRUE) OREL b(TRUE), new line));
|
||||
|
||||
print(("F ANDTH F = ", a(FALSE) ANDTH b(FALSE), new line));
|
||||
print(("F ANDTH T = ", a(FALSE) ANDTH b(TRUE), new line));
|
||||
|
||||
print(("F OREL F = ", a(FALSE) OREL b(FALSE), new line));
|
||||
print(("F OREL T = ", a(FALSE) OREL b(TRUE), new line));
|
||||
|
||||
print(("T ANDTH F = ", a(TRUE) ANDTH b(FALSE), new line));
|
||||
print(("T ANDTH T = ", a(TRUE) ANDTH b(TRUE), new line))
|
||||
|
||||
CO;
|
||||
# Valid for Algol 68G and 68C using non standard operators #
|
||||
print(("T ORF F = ", a(TRUE) ORF b(FALSE), new line));
|
||||
print(("F ANDF T = ", a(FALSE) ANDF b(TRUE), new line))
|
||||
END CO
|
||||
|
||||
)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
begin
|
||||
|
||||
logical procedure a( logical value v ) ; begin write( "a: ", v ); v end ;
|
||||
logical procedure b( logical value v ) ; begin write( "b: ", v ); v end ;
|
||||
|
||||
write( "and: ", a( true ) and b( true ) );
|
||||
write( "---" );
|
||||
write( "or: ", a( true ) or b( true ) );
|
||||
write( "---" );
|
||||
write( "and: ", a( false ) and b( true ) );
|
||||
write( "---" );
|
||||
write( "or: ", a( false ) or b( true ) );
|
||||
write( "---" );
|
||||
|
||||
end.
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/awk -f
|
||||
BEGIN {
|
||||
print (a(1) && b(1))
|
||||
print (a(1) || b(1))
|
||||
print (a(0) && b(1))
|
||||
print (a(0) || b(1))
|
||||
}
|
||||
|
||||
|
||||
function a(x) {
|
||||
print " x:"x
|
||||
return x
|
||||
}
|
||||
function b(y) {
|
||||
print " y:"y
|
||||
return y
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
BYTE FUNC a(BYTE x)
|
||||
PrintF(" a(%B)",x)
|
||||
RETURN (x)
|
||||
|
||||
BYTE FUNC b(BYTE x)
|
||||
PrintF(" b(%B)",x)
|
||||
RETURN (x)
|
||||
|
||||
PROC Main()
|
||||
BYTE i,j
|
||||
|
||||
FOR i=0 TO 1
|
||||
DO
|
||||
FOR j=0 TO 1
|
||||
DO
|
||||
PrintF("Calculating %B AND %B: call",i,j)
|
||||
IF a(i)=1 AND b(j)=1 THEN
|
||||
FI
|
||||
PutE()
|
||||
OD
|
||||
OD
|
||||
PutE()
|
||||
|
||||
FOR i=0 TO 1
|
||||
DO
|
||||
FOR j=0 TO 1
|
||||
DO
|
||||
PrintF("Calculating %B OR %B: call",i,j)
|
||||
IF a(i)=1 OR b(j)=1 THEN
|
||||
FI
|
||||
PutE()
|
||||
OD
|
||||
OD
|
||||
RETURN
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Test_Short_Circuit is
|
||||
function A (Value : Boolean) return Boolean is
|
||||
begin
|
||||
Put (" A=" & Boolean'Image (Value));
|
||||
return Value;
|
||||
end A;
|
||||
function B (Value : Boolean) return Boolean is
|
||||
begin
|
||||
Put (" B=" & Boolean'Image (Value));
|
||||
return Value;
|
||||
end B;
|
||||
begin
|
||||
for I in Boolean'Range loop
|
||||
for J in Boolean'Range loop
|
||||
Put (" (A and then B)=" & Boolean'Image (A (I) and then B (J)));
|
||||
New_Line;
|
||||
end loop;
|
||||
end loop;
|
||||
for I in Boolean'Range loop
|
||||
for J in Boolean'Range loop
|
||||
Put (" (A or else B)=" & Boolean'Image (A (I) or else B (J)));
|
||||
New_Line;
|
||||
end loop;
|
||||
end loop;
|
||||
end Test_Short_Circuit;
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
on run
|
||||
|
||||
map(test, {|and|, |or|})
|
||||
|
||||
end run
|
||||
|
||||
-- test :: ((Bool, Bool) -> Bool) -> (Bool, Bool, Bool, Bool)
|
||||
on test(f)
|
||||
map(f, {{true, true}, {true, false}, {false, true}, {false, false}})
|
||||
end test
|
||||
|
||||
|
||||
|
||||
-- |and| :: (Bool, Bool) -> Bool
|
||||
on |and|(tuple)
|
||||
set {x, y} to tuple
|
||||
|
||||
a(x) and b(y)
|
||||
end |and|
|
||||
|
||||
-- |or| :: (Bool, Bool) -> Bool
|
||||
on |or|(tuple)
|
||||
set {x, y} to tuple
|
||||
|
||||
a(x) or b(y)
|
||||
end |or|
|
||||
|
||||
-- a :: Bool -> Bool
|
||||
on a(bool)
|
||||
log "a"
|
||||
return bool
|
||||
end a
|
||||
|
||||
-- b :: Bool -> Bool
|
||||
on b(bool)
|
||||
log "b"
|
||||
return bool
|
||||
end b
|
||||
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
script mf
|
||||
property lambda : f
|
||||
end script
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to mf's lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end map
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
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]
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
i = 1
|
||||
j = 1
|
||||
x := a(i) and b(j)
|
||||
y := a(i) or b(j)
|
||||
|
||||
a(p)
|
||||
{
|
||||
MsgBox, a() was called with the parameter "%p%".
|
||||
Return, p
|
||||
}
|
||||
|
||||
b(p)
|
||||
{
|
||||
MsgBox, b() was called with the parameter "%p%".
|
||||
Return, p
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
TEST(0,0)
|
||||
TEST(0,1)
|
||||
TEST(1,0)
|
||||
TEST(1,1)
|
||||
Return
|
||||
|
||||
Lbl TEST
|
||||
r₁→X
|
||||
r₂→Y
|
||||
Disp X▶Hex+3," and ",Y▶Hex+3," = ",(A(X)?B(Y))▶Hex+3,i
|
||||
Disp X▶Hex+3," or ",Y▶Hex+3," = ",(A(X)??B(Y))▶Hex+3,i
|
||||
.Wait for keypress
|
||||
getKeyʳ
|
||||
Return
|
||||
|
||||
Lbl A
|
||||
r₁
|
||||
Return
|
||||
|
||||
Lbl B
|
||||
r₁
|
||||
Return
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
REM TRUE is represented as -1, FALSE as 0
|
||||
FOR i% = TRUE TO FALSE
|
||||
FOR j% = TRUE TO FALSE
|
||||
PRINT "For x=a(";FNboolstring(i%);") AND b(";FNboolstring(j%);")"
|
||||
x% = FALSE
|
||||
REM Short-circuit AND can be simulated by cascaded IFs:
|
||||
IF FNa(i%) IF FNb(j%) THEN x%=TRUE
|
||||
PRINT "x is ";FNboolstring(x%)
|
||||
PRINT
|
||||
PRINT "For y=a(";FNboolstring(i%);") OR b(";FNboolstring(j%);")"
|
||||
y% = FALSE
|
||||
REM Short-circuit OR can be simulated by De Morgan's laws:
|
||||
IF NOTFNa(i%) IF NOTFNb(j%) ELSE y%=TRUE : REM Note ELSE without THEN
|
||||
PRINT "y is ";FNboolstring(y%)
|
||||
PRINT
|
||||
NEXT:NEXT
|
||||
END
|
||||
|
||||
DEFFNa(bool%)
|
||||
PRINT "Function A used; ";
|
||||
=bool%
|
||||
|
||||
DEFFNb(bool%)
|
||||
PRINT "Function B used; ";
|
||||
=bool%
|
||||
|
||||
DEFFNboolstring(bool%)
|
||||
IF bool%=0 THEN ="FALSE" ELSE="TRUE"
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
' Short-circuit evaluation
|
||||
FUNCTION a(f)
|
||||
PRINT "FUNCTION a"
|
||||
RETURN f
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION b(f)
|
||||
PRINT "FUNCTION b"
|
||||
RETURN f
|
||||
END FUNCTION
|
||||
|
||||
PRINT "FALSE and TRUE"
|
||||
x = a(FALSE) AND b(TRUE)
|
||||
PRINT x
|
||||
|
||||
PRINT "TRUE and TRUE"
|
||||
x = a(TRUE) AND b(TRUE)
|
||||
PRINT x
|
||||
|
||||
PRINT "FALSE or FALSE"
|
||||
y = a(FALSE) OR b(FALSE)
|
||||
PRINT y
|
||||
|
||||
PRINT "TRUE or FALSE"
|
||||
y = a(TRUE) OR b(FALSE)
|
||||
PRINT y
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
%=== Batch Files have no booleans on if command, let alone short-circuit evaluation ===%
|
||||
%=== I will instead use 1 as true and 0 as false. ===%
|
||||
|
||||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
echo AND
|
||||
for /l %%i in (0,1,1) do (
|
||||
for /l %%j in (0,1,1) do (
|
||||
echo.a^(%%i^) AND b^(%%j^)
|
||||
call :a %%i
|
||||
set res=!bool_a!
|
||||
if not !res!==0 (
|
||||
call :b %%j
|
||||
set res=!bool_b!
|
||||
)
|
||||
echo.=^> !res!
|
||||
)
|
||||
)
|
||||
|
||||
echo ---------------------------------
|
||||
echo OR
|
||||
for /l %%i in (0,1,1) do (
|
||||
for /l %%j in (0,1,1) do (
|
||||
echo a^(%%i^) OR b^(%%j^)
|
||||
call :a %%i
|
||||
set res=!bool_a!
|
||||
if !res!==0 (
|
||||
call :b %%j
|
||||
set res=!bool_b!
|
||||
)
|
||||
echo.=^> !res!
|
||||
)
|
||||
)
|
||||
pause>nul
|
||||
exit /b 0
|
||||
|
||||
|
||||
::----------------------------------------
|
||||
:a
|
||||
echo. calls func a
|
||||
set bool_a=%1
|
||||
goto :EOF
|
||||
|
||||
:b
|
||||
echo. calls func b
|
||||
set bool_b=%1
|
||||
goto :EOF
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
( (a=.out$"I'm a"&!!arg)
|
||||
& (b=.out$"I'm b"&!!arg)
|
||||
& (false==~)
|
||||
& (true==)
|
||||
& !false !true:?outer
|
||||
& whl
|
||||
' ( !outer:%?x ?outer
|
||||
& !false !true:?inner
|
||||
& whl
|
||||
' ( !inner:%?y ?inner
|
||||
& out
|
||||
$ ( Testing
|
||||
(!!x&true|false)
|
||||
AND
|
||||
(!!y&true|false)
|
||||
)
|
||||
& `(a$!x&b$!y)
|
||||
& out
|
||||
$ ( Testing
|
||||
(!!x&true|false)
|
||||
OR
|
||||
(!!y&true|false)
|
||||
)
|
||||
& `(a$!x|b$!y)
|
||||
)
|
||||
)
|
||||
& done
|
||||
);
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
#include <iostream>
|
||||
|
||||
bool a(bool in)
|
||||
{
|
||||
std::cout << "a" << std::endl;
|
||||
return in;
|
||||
}
|
||||
|
||||
bool b(bool in)
|
||||
{
|
||||
std::cout << "b" << std::endl;
|
||||
return in;
|
||||
}
|
||||
|
||||
void test(bool i, bool j) {
|
||||
std::cout << std::boolalpha << i << " and " << j << " = " << (a(i) && b(j)) << std::endl;
|
||||
std::cout << std::boolalpha << i << " or " << j << " = " << (a(i) || b(j)) << std::endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test(false, false);
|
||||
test(false, true);
|
||||
test(true, false);
|
||||
test(true, true);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
alias a eval \''echo "Called a \!:1"; "\!:1"'\'
|
||||
alias b eval \''echo "Called b \!:1"; "\!:1"'\'
|
||||
|
||||
foreach i (false true)
|
||||
foreach j (false true)
|
||||
a $i && b $j && set x=true || set x=false
|
||||
echo " $i && $j is $x"
|
||||
|
||||
a $i || b $j && set x=true || set x=false
|
||||
echo " $i || $j is $x"
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# Succeeds, only prints "ok".
|
||||
if ( 1 || { echo This command never runs. } ) echo ok
|
||||
|
||||
# Fails, aborts shell with "bad: Undefined variable".
|
||||
if ( 1 || $bad ) echo ok
|
||||
|
||||
# Prints "error", then "ok".
|
||||
if ( 1 || `echo error >/dev/stderr` ) echo ok
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static bool a(bool value)
|
||||
{
|
||||
Console.WriteLine("a");
|
||||
return value;
|
||||
}
|
||||
|
||||
static bool b(bool value)
|
||||
{
|
||||
Console.WriteLine("b");
|
||||
return value;
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
foreach (var i in new[] { false, true })
|
||||
{
|
||||
foreach (var j in new[] { false, true })
|
||||
{
|
||||
Console.WriteLine("{0} and {1} = {2}", i, j, a(i) && b(j));
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("{0} or {1} = {2}", i, j, a(i) || b(j));
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
a
|
||||
False and False = False
|
||||
|
||||
a
|
||||
b
|
||||
False or False = False
|
||||
|
||||
a
|
||||
False and True = False
|
||||
|
||||
a
|
||||
b
|
||||
False or True = True
|
||||
|
||||
a
|
||||
b
|
||||
True and False = False
|
||||
|
||||
a
|
||||
True or False = True
|
||||
|
||||
a
|
||||
b
|
||||
True and True = True
|
||||
|
||||
a
|
||||
True or True = True
|
||||
32
Task/Short-circuit-evaluation/C/short-circuit-evaluation.c
Normal file
32
Task/Short-circuit-evaluation/C/short-circuit-evaluation.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool a(bool in)
|
||||
{
|
||||
printf("I am a\n");
|
||||
return in;
|
||||
}
|
||||
|
||||
bool b(bool in)
|
||||
{
|
||||
printf("I am b\n");
|
||||
return in;
|
||||
}
|
||||
|
||||
#define TEST(X,Y,O) \
|
||||
do { \
|
||||
x = a(X) O b(Y); \
|
||||
printf(#X " " #O " " #Y " = %s\n\n", x ? "true" : "false"); \
|
||||
} while(false);
|
||||
|
||||
int main()
|
||||
{
|
||||
bool x;
|
||||
|
||||
TEST(false, true, &&); // b is not evaluated
|
||||
TEST(true, false, ||); // b is not evaluated
|
||||
TEST(true, false, &&); // b is evaluated
|
||||
TEST(false, false, ||); // b is evaluated
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(letfn [(a [bool] (print "(a)") bool)
|
||||
(b [bool] (print "(b)") bool)]
|
||||
(doseq [i [true false] j [true false]]
|
||||
(print i "OR" j "= ")
|
||||
(println (or (a i) (b j)))
|
||||
(print i "AND" j " = ")
|
||||
(println (and (a i) (b j)))))
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
(defun a (F)
|
||||
(print 'a)
|
||||
F )
|
||||
|
||||
(defun b (F)
|
||||
(print 'b)
|
||||
F )
|
||||
|
||||
(dolist (x '((nil nil) (nil T) (T T) (T nil)))
|
||||
(format t "~%(and ~S)" x)
|
||||
(and (a (car x)) (b (car(cdr x))))
|
||||
(format t "~%(or ~S)" x)
|
||||
(or (a (car x)) (b (car(cdr x)))))
|
||||
21
Task/Short-circuit-evaluation/D/short-circuit-evaluation.d
Normal file
21
Task/Short-circuit-evaluation/D/short-circuit-evaluation.d
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import std.stdio, std.algorithm;
|
||||
|
||||
T a(T)(T answer) {
|
||||
writefln(" # Called function a(%s) -> %s", answer, answer);
|
||||
return answer;
|
||||
}
|
||||
|
||||
T b(T)(T answer) {
|
||||
writefln(" # Called function b(%s) -> %s", answer, answer);
|
||||
return answer;
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (immutable x, immutable y;
|
||||
[false, true].cartesianProduct([false, true])) {
|
||||
writeln("\nCalculating: r1 = a(x) && b(y)");
|
||||
immutable r1 = a(x) && b(y);
|
||||
writeln("Calculating: r2 = a(x) || b(y)");
|
||||
immutable r2 = a(x) || b(y);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
program ShortCircuitEvaluation;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses SysUtils;
|
||||
|
||||
function A(aValue: Boolean): Boolean;
|
||||
begin
|
||||
Writeln('a');
|
||||
Result := aValue;
|
||||
end;
|
||||
|
||||
function B(aValue: Boolean): Boolean;
|
||||
begin
|
||||
Writeln('b');
|
||||
Result := aValue;
|
||||
end;
|
||||
|
||||
var
|
||||
i, j: Boolean;
|
||||
begin
|
||||
for i in [False, True] do
|
||||
begin
|
||||
for j in [False, True] do
|
||||
begin
|
||||
Writeln(Format('%s and %s = %s', [BoolToStr(i, True), BoolToStr(j, True), BoolToStr(A(i) and B(j), True)]));
|
||||
Writeln;
|
||||
Writeln(Format('%s or %s = %s', [BoolToStr(i, True), BoolToStr(j, True), BoolToStr(A(i) or B(j), True)]));
|
||||
Writeln;
|
||||
end;
|
||||
end;
|
||||
end.
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
func a(v) {
|
||||
print(nameof(a), terminator: "")
|
||||
return v
|
||||
}
|
||||
|
||||
func b(v) {
|
||||
print(nameof(b), terminator: "")
|
||||
return v
|
||||
}
|
||||
|
||||
func testMe(i, j) {
|
||||
print("Testing a(\(i)) && b(\(j))")
|
||||
print("Trace: ", terminator: "")
|
||||
print("\nResult: \(a(i) && b(j))")
|
||||
|
||||
print("Testing a(\(i)) || b(\(j))")
|
||||
print("Trace: ", terminator: "")
|
||||
print("\nResult: \(a(i) || b(j))")
|
||||
|
||||
print()
|
||||
}
|
||||
|
||||
testMe(false, false)
|
||||
testMe(false, true)
|
||||
testMe(true, false)
|
||||
testMe(true, true)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
def a(v) { println("a"); return v }
|
||||
def b(v) { println("b"); return v }
|
||||
|
||||
def x := a(i) && b(j)
|
||||
def y := b(i) || b(j)
|
||||
|
|
@ -0,0 +1 @@
|
|||
def x := a(i) && (def funky := b(j))
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
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 };
|
||||
|
||||
public program()
|
||||
{
|
||||
boolValues.forEach:(bool i)
|
||||
{
|
||||
boolValues.forEach:(bool j)
|
||||
{
|
||||
console.printLine(i," and ",j," = ",a(i) && b(j));
|
||||
|
||||
console.writeLine();
|
||||
console.printLine(i," or ",j," = ",a(i) || b(j));
|
||||
console.writeLine()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
defmodule Short_circuit do
|
||||
defp a(bool) do
|
||||
IO.puts "a( #{bool} ) called"
|
||||
bool
|
||||
end
|
||||
|
||||
defp b(bool) do
|
||||
IO.puts "b( #{bool} ) called"
|
||||
bool
|
||||
end
|
||||
|
||||
def task do
|
||||
Enum.each([true, false], fn i ->
|
||||
Enum.each([true, false], fn j ->
|
||||
IO.puts "a( #{i} ) and b( #{j} ) is #{a(i) and b(j)}.\n"
|
||||
IO.puts "a( #{i} ) or b( #{j} ) is #{a(i) or b(j)}.\n"
|
||||
end)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
Short_circuit.task
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
-module( short_circuit_evaluation ).
|
||||
|
||||
-export( [task/0] ).
|
||||
|
||||
task() ->
|
||||
[task_helper(X, Y) || X <- [true, false], Y <- [true, false]].
|
||||
|
||||
|
||||
|
||||
a( Boolean ) ->
|
||||
io:fwrite( " a ~p~n", [Boolean] ),
|
||||
Boolean.
|
||||
|
||||
b( Boolean ) ->
|
||||
io:fwrite( " b ~p~n", [Boolean] ),
|
||||
Boolean.
|
||||
|
||||
task_helper( Boolean1, Boolean2 ) ->
|
||||
io:fwrite( "~p andalso ~p~n", [Boolean1, Boolean2] ),
|
||||
io:fwrite( "=> ~p~n", [a(Boolean1) andalso b(Boolean2)] ),
|
||||
io:fwrite( "~p orelse ~p~n", [Boolean1, Boolean2] ),
|
||||
io:fwrite( "=> ~p~n", [a(Boolean1) orelse b(Boolean2)] ).
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
let a (x : bool) = printf "(a)"; x
|
||||
let b (x : bool) = printf "(b)"; x
|
||||
|
||||
[for x in [true; false] do for y in [true; false] do yield (x, y)]
|
||||
|> List.iter (fun (x, y) ->
|
||||
printfn "%b AND %b = %b" x y ((a x) && (b y))
|
||||
printfn "%b OR %b = %b" x y ((a x) || (b y)))
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
USING: combinators.short-circuit.smart io prettyprint ;
|
||||
IN: rosetta-code.short-circuit
|
||||
|
||||
: a ( ? -- ? ) "(a)" write ;
|
||||
: b ( ? -- ? ) "(b)" write ;
|
||||
|
||||
"f && f = " write { [ f a ] [ f b ] } && .
|
||||
"f || f = " write { [ f a ] [ f b ] } || .
|
||||
"f && t = " write { [ f a ] [ t b ] } && .
|
||||
"f || t = " write { [ f a ] [ t b ] } || .
|
||||
"t && f = " write { [ t a ] [ f b ] } && .
|
||||
"t || f = " write { [ t a ] [ f b ] } || .
|
||||
"t && t = " write { [ t a ] [ t b ] } && .
|
||||
"t || t = " write { [ t a ] [ t b ] } || .
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
class Main
|
||||
{
|
||||
static Bool a (Bool value)
|
||||
{
|
||||
echo ("in a")
|
||||
return value
|
||||
}
|
||||
|
||||
static Bool b (Bool value)
|
||||
{
|
||||
echo ("in b")
|
||||
return value
|
||||
}
|
||||
|
||||
public static Void main ()
|
||||
{
|
||||
[false,true].each |i|
|
||||
{
|
||||
[false,true].each |j|
|
||||
{
|
||||
Bool result := a(i) && b(j)
|
||||
echo ("a($i) && b($j): " + result)
|
||||
result = a(i) || b(j)
|
||||
echo ("a($i) || b($j): " + result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
\ Short-circuit evaluation definitions from Wil Baden, with minor name changes
|
||||
: ENDIF postpone THEN ; immediate
|
||||
|
||||
: COND 0 ; immediate
|
||||
: ENDIFS BEGIN DUP WHILE postpone ENDIF REPEAT DROP ; immediate
|
||||
: ORELSE s" ?DUP 0= IF" evaluate ; immediate
|
||||
: ANDIF s" DUP IF DROP" evaluate ; immediate
|
||||
|
||||
: .bool IF ." true " ELSE ." false " THEN ;
|
||||
: A ." A=" DUP .bool ;
|
||||
: B ." B=" DUP .bool ;
|
||||
|
||||
: test
|
||||
CR
|
||||
1 -1 DO 1 -1 DO
|
||||
COND I A ANDIF J B ENDIFS ." ANDIF=" .bool CR
|
||||
COND I A ORELSE J B ENDIFS ." ORELSE=" .bool CR
|
||||
LOOP LOOP ;
|
||||
|
||||
\ An alternative based on explicitly short-circuiting conditionals, Dave Keenan
|
||||
: END-PRIOR-IF 1 CS-ROLL postpone ENDIF ; immediate
|
||||
|
||||
: test
|
||||
CR
|
||||
1 -1 DO 1 -1 DO
|
||||
I A IF J B IF 1 ELSE END-PRIOR-IF 0 ENDIF ." ANDIF=" .bool CR
|
||||
I A 0= IF J B IF END-PRIOR-IF 1 ELSE 0 ENDIF ." ORELSE=" .bool CR
|
||||
LOOP LOOP ;
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
program Short_Circuit_Eval
|
||||
implicit none
|
||||
|
||||
logical :: x, y
|
||||
logical, dimension(2) :: l = (/ .false., .true. /)
|
||||
integer :: i, j
|
||||
|
||||
do i = 1, 2
|
||||
do j = 1, 2
|
||||
write(*, "(a,l1,a,l1,a)") "Calculating x = a(", l(i), ") and b(", l(j), ")"
|
||||
! a AND b
|
||||
x = a(l(i))
|
||||
if(x) then
|
||||
x = b(l(j))
|
||||
write(*, "(a,l1)") "x = ", x
|
||||
else
|
||||
write(*, "(a,l1)") "x = ", x
|
||||
end if
|
||||
|
||||
write(*,*)
|
||||
write(*, "(a,l1,a,l1,a)") "Calculating y = a(", l(i), ") or b(", l(j), ")"
|
||||
! a OR b
|
||||
y = a(l(i))
|
||||
if(y) then
|
||||
write(*, "(a,l1)") "y = ", y
|
||||
else
|
||||
y = b(l(j))
|
||||
write(*, "(a,l1)") "y = ", y
|
||||
end if
|
||||
write(*,*)
|
||||
end do
|
||||
end do
|
||||
|
||||
contains
|
||||
|
||||
function a(value)
|
||||
logical :: a
|
||||
logical, intent(in) :: value
|
||||
|
||||
a = value
|
||||
write(*, "(a,l1,a)") "Called function a(", value, ")"
|
||||
end function
|
||||
|
||||
function b(value)
|
||||
logical :: b
|
||||
logical, intent(in) :: value
|
||||
|
||||
b = value
|
||||
write(*, "(a,l1,a)") "Called function b(", value, ")"
|
||||
end function
|
||||
end program
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Function a(p As Boolean) As Boolean
|
||||
Print "a() called"
|
||||
Return p
|
||||
End Function
|
||||
|
||||
Function b(p As Boolean) As Boolean
|
||||
Print "b() called"
|
||||
Return p
|
||||
End Function
|
||||
|
||||
Dim As Boolean i, j, x, y
|
||||
i = False
|
||||
j = True
|
||||
Print "Without short-circuit evaluation :"
|
||||
Print
|
||||
x = a(i) And b(j)
|
||||
y = a(i) Or b(j)
|
||||
Print "x = "; x; " y = "; y
|
||||
Print
|
||||
Print "With short-circuit evaluation :"
|
||||
Print
|
||||
x = a(i) AndAlso b(j) '' b(j) not called as a(i) = false and so x must be false
|
||||
y = a(i) OrElse b(j) '' b(j) still called as can't determine y unless it is
|
||||
Print "x = "; x; " y = "; y
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
32
Task/Short-circuit-evaluation/Go/short-circuit-evaluation.go
Normal file
32
Task/Short-circuit-evaluation/Go/short-circuit-evaluation.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func a(v bool) bool {
|
||||
fmt.Print("a")
|
||||
return v
|
||||
}
|
||||
|
||||
func b(v bool) bool {
|
||||
fmt.Print("b")
|
||||
return v
|
||||
}
|
||||
|
||||
func test(i, j bool) {
|
||||
fmt.Printf("Testing a(%t) && b(%t)\n", i, j)
|
||||
fmt.Print("Trace: ")
|
||||
fmt.Println("\nResult:", a(i) && b(j))
|
||||
|
||||
fmt.Printf("Testing a(%t) || b(%t)\n", i, j)
|
||||
fmt.Print("Trace: ")
|
||||
fmt.Println("\nResult:", a(i) || b(j))
|
||||
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(false, false)
|
||||
test(false, true)
|
||||
test(true, false)
|
||||
test(true, true)
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
def f = { println ' AHA!'; it instanceof String }
|
||||
def g = { printf ('%5d ', it); it > 50 }
|
||||
|
||||
println 'bitwise'
|
||||
assert g(100) & f('sss')
|
||||
assert g(2) | f('sss')
|
||||
assert ! (g(1) & f('sss'))
|
||||
assert g(200) | f('sss')
|
||||
|
||||
println '''
|
||||
logical'''
|
||||
assert g(100) && f('sss')
|
||||
assert g(2) || f('sss')
|
||||
assert ! (g(1) && f('sss'))
|
||||
assert g(200) || f('sss')
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
module ShortCircuit where
|
||||
|
||||
import Prelude hiding ((&&), (||))
|
||||
import Debug.Trace
|
||||
|
||||
False && _ = False
|
||||
True && False = False
|
||||
_ && _ = True
|
||||
|
||||
True || _ = True
|
||||
False || True = True
|
||||
_ || _ = False
|
||||
|
||||
a p = trace ("<a " ++ show p ++ ">") p
|
||||
b p = trace ("<b " ++ show p ++ ">") p
|
||||
|
||||
main = mapM_ print ( [ a p || b q | p <- [False, True], q <- [False, True] ]
|
||||
++ [ a p && b q | p <- [False, True], q <- [False, True] ])
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
_ && False = False
|
||||
False && True = False
|
||||
_ && _ = True
|
||||
|
||||
_ || True = True
|
||||
True || False = True
|
||||
_ || _ = False
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
p && q = case p of
|
||||
False -> False
|
||||
_ -> case q of
|
||||
False -> False
|
||||
_ -> True
|
||||
|
||||
p || q = case p of
|
||||
True -> True
|
||||
_ -> case q of
|
||||
True -> True
|
||||
_ -> False
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
procedure main()
|
||||
&trace := -1 # ensures functions print their names
|
||||
|
||||
every (i := false | true ) & ( j := false | true) do {
|
||||
write("i,j := ",image(i),", ",image(j))
|
||||
write("i & j:")
|
||||
x := i() & j() # invoke true/false
|
||||
write("i | j:")
|
||||
y := i() | j() # invoke true/false
|
||||
}
|
||||
end
|
||||
|
||||
procedure true() #: succeeds always (returning null)
|
||||
return
|
||||
end
|
||||
|
||||
procedure false() #: fails always
|
||||
fail # for clarity but not needed as running into end has the same effect
|
||||
end
|
||||
19
Task/Short-circuit-evaluation/Io/short-circuit-evaluation.io
Normal file
19
Task/Short-circuit-evaluation/Io/short-circuit-evaluation.io
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
a := method(bool,
|
||||
writeln("a(#{bool}) called." interpolate)
|
||||
bool
|
||||
)
|
||||
b := method(bool,
|
||||
writeln("b(#{bool}) called." interpolate)
|
||||
bool
|
||||
)
|
||||
|
||||
list(true,false) foreach(avalue,
|
||||
list(true,false) foreach(bvalue,
|
||||
x := a(avalue) and b(bvalue)
|
||||
writeln("x = a(#{avalue}) and b(#{bvalue}) is #{x}" interpolate)
|
||||
writeln
|
||||
y := a(avalue) or b(bvalue)
|
||||
writeln("y = a(#{avalue}) or b(#{bvalue}) is #{y}" interpolate)
|
||||
writeln
|
||||
)
|
||||
)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
labeled=:1 :'[ smoutput@,&":~&m'
|
||||
A=: 'A ' labeled
|
||||
B=: 'B ' labeled
|
||||
and=: ^:
|
||||
or=: 2 :'u^:(-.@v)'
|
||||
14
Task/Short-circuit-evaluation/J/short-circuit-evaluation-2.j
Normal file
14
Task/Short-circuit-evaluation/J/short-circuit-evaluation-2.j
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(A and B) 1
|
||||
B 1
|
||||
A 1
|
||||
1
|
||||
(A and B) 0
|
||||
B 0
|
||||
0
|
||||
(A or B) 1
|
||||
B 1
|
||||
1
|
||||
(A or B) 0
|
||||
B 0
|
||||
A 0
|
||||
0
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
public class ShortCirc {
|
||||
public static void main(String[] args){
|
||||
System.out.println("F and F = " + (a(false) && b(false)) + "\n");
|
||||
System.out.println("F or F = " + (a(false) || b(false)) + "\n");
|
||||
|
||||
System.out.println("F and T = " + (a(false) && b(true)) + "\n");
|
||||
System.out.println("F or T = " + (a(false) || b(true)) + "\n");
|
||||
|
||||
System.out.println("T and F = " + (a(true) && b(false)) + "\n");
|
||||
System.out.println("T or F = " + (a(true) || b(false)) + "\n");
|
||||
|
||||
System.out.println("T and T = " + (a(true) && b(true)) + "\n");
|
||||
System.out.println("T or T = " + (a(true) || b(true)) + "\n");
|
||||
}
|
||||
|
||||
public static boolean a(boolean a){
|
||||
System.out.println("a");
|
||||
return a;
|
||||
}
|
||||
|
||||
public static boolean b(boolean b){
|
||||
System.out.println("b");
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
function a(bool) {
|
||||
console.log('a -->', bool);
|
||||
|
||||
return bool;
|
||||
}
|
||||
|
||||
function b(bool) {
|
||||
console.log('b -->', bool);
|
||||
|
||||
return bool;
|
||||
}
|
||||
|
||||
|
||||
var x = a(false) && b(true),
|
||||
y = a(true) || b(false),
|
||||
z = true ? a(true) : b(false);
|
||||
|
||||
return [x, y, z];
|
||||
})();
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
def a(x): " a(\(x))" | stderr | x;
|
||||
|
||||
def b(y): " b(\(y))" | stderr | y;
|
||||
|
||||
"and:", (a(true) and b(true)),
|
||||
"or:", (a(true) or b(true)),
|
||||
"and:", (a(false) and b(true)),
|
||||
"or:", (a(false) or b(true))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
$ jq -r -n -f Short-circuit-evaluation.jq
|
||||
and:
|
||||
" a(true)"
|
||||
" b(true)"
|
||||
true
|
||||
or:
|
||||
" a(true)"
|
||||
true
|
||||
and:
|
||||
" a(false)"
|
||||
false
|
||||
or:
|
||||
" a(false)"
|
||||
" b(true)"
|
||||
true
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
a(x) = (println("\t# Called a($x)"); return x)
|
||||
b(x) = (println("\t# Called b($x)"); return x)
|
||||
|
||||
for i in [true,false], j in [true, false]
|
||||
println("\nCalculating: x = a($i) && b($j)"); x = a(i) && b(j)
|
||||
println("\tResult: x = $x")
|
||||
println("\nCalculating: y = a($i) || b($j)"); y = a(i) || b(j)
|
||||
println("\tResult: y = $y")
|
||||
end
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
// version 1.1.2
|
||||
|
||||
fun a(v: Boolean): Boolean {
|
||||
println("'a' called")
|
||||
return v
|
||||
}
|
||||
|
||||
fun b(v: Boolean): Boolean {
|
||||
println("'b' called")
|
||||
return v
|
||||
}
|
||||
|
||||
fun main(args: Array<String>){
|
||||
val pairs = arrayOf(Pair(true, true), Pair(true, false), Pair(false, true), Pair(false, false))
|
||||
for (pair in pairs) {
|
||||
val x = a(pair.first) && b(pair.second)
|
||||
println("${pair.first} && ${pair.second} = $x")
|
||||
val y = a(pair.first) || b(pair.second)
|
||||
println("${pair.first} || ${pair.second} = $y")
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
{def A {lambda {:bool} :bool}} -> A
|
||||
{def B {lambda {:bool} :bool}} -> B
|
||||
|
||||
{and {A true} {B true}} -> true
|
||||
{and {A true} {B false}} -> false
|
||||
{and {A false} {B true}} -> false
|
||||
{and {A false} {B false}} -> false
|
||||
|
||||
{or {A true} {B true}} -> true
|
||||
{or {A true} {B false}} -> true
|
||||
{or {A false} {B true}} -> true
|
||||
{or {A false} {B false}} -> false
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
{def fib
|
||||
{lambda {:n}
|
||||
{if {< :n 2}
|
||||
then 1
|
||||
else {+ {fib {- :n 1}} {fib {- :n 2}}}}}}
|
||||
-> fib
|
||||
|
||||
1) Using the if special form:
|
||||
|
||||
{if true then {+ 1 2} else {fib 29}}
|
||||
-> 3 // {fib 29} is not evaluated
|
||||
|
||||
{if false then {+ 1 2} else {fib 29}}
|
||||
-> 832040 // {fib 29} is evaluated in 5847ms
|
||||
|
||||
2) The if special form can't be simply replaced by a pair:
|
||||
|
||||
{def when {P.new {+ 1 2} {fib 29}}} // inner expressions are
|
||||
{P.left {when}} -> 3 // both evaluated before
|
||||
{P.right {when}} -> 832040 // and we don't want that
|
||||
|
||||
3) We can delay evaluation using lambdas:
|
||||
|
||||
{def when
|
||||
{P.new {lambda {} {+ 1 2}} // will return a lambda
|
||||
{lambda {} {fib 22}} }} // to be evaluated later
|
||||
-> when
|
||||
{{P.left {when}}} -> 3 // lambdas are evaluated
|
||||
{{P.right {when}}} -> 832040 // after choice using {}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
print "AND"
|
||||
for i = 0 to 1
|
||||
for j = 0 to 1
|
||||
print "a("; i; ") AND b( "; j; ")"
|
||||
res =a( i) 'call always
|
||||
if res <>0 then 'short circuit if 0
|
||||
res = b( j)
|
||||
end if
|
||||
print "=>",res
|
||||
next
|
||||
next
|
||||
|
||||
print "---------------------------------"
|
||||
print "OR"
|
||||
for i = 0 to 1
|
||||
for j = 0 to 1
|
||||
print "a("; i; ") OR b("; j; ")"
|
||||
res =a( i) 'call always
|
||||
if res = 0 then 'short circuit if <>0
|
||||
res = b( j)
|
||||
end if
|
||||
print "=>", res
|
||||
next
|
||||
next
|
||||
|
||||
'----------------------------------------
|
||||
function a( t)
|
||||
print ,"calls func a"
|
||||
a = t
|
||||
end function
|
||||
|
||||
function b( t)
|
||||
print ,"calls func b"
|
||||
b = t
|
||||
end function
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
global outcome
|
||||
function a bool
|
||||
put "a called with" && bool & cr after outcome
|
||||
return bool
|
||||
end a
|
||||
function b bool
|
||||
put "b called with" && bool & cr after outcome
|
||||
return bool
|
||||
end b
|
||||
|
||||
on mouseUp
|
||||
local tExp
|
||||
put empty into outcome
|
||||
repeat for each item op in "and,or"
|
||||
repeat for each item x in "true,false"
|
||||
put merge("a([[x]]) [[op]] b([[x]])") into tExp
|
||||
put merge(tExp && "is [[" & tExp & "]]") & cr after outcome
|
||||
put merge("a([[x]]) [[op]] b([[not x]])") into tExp
|
||||
put merge(tExp && "is [[" & tExp & "]]") & cr after outcome
|
||||
end repeat
|
||||
put cr after outcome
|
||||
end repeat
|
||||
put outcome
|
||||
end mouseUp
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
and [notequal? :x 0] [1/:x > 3]
|
||||
(or [:x < 0] [:y < 0] [sqrt :x + sqrt :y < 3])
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
function a(i)
|
||||
print "Function a(i) called."
|
||||
return i
|
||||
end
|
||||
|
||||
function b(i)
|
||||
print "Function b(i) called."
|
||||
return i
|
||||
end
|
||||
|
||||
i = true
|
||||
x = a(i) and b(i); print ""
|
||||
y = a(i) or b(i); print ""
|
||||
|
||||
i = false
|
||||
x = a(i) and b(i); print ""
|
||||
y = a(i) or b(i)
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
function x=a(x)
|
||||
printf('a: %i\n',x);
|
||||
end;
|
||||
function x=b(x)
|
||||
printf('b: %i\n',x);
|
||||
end;
|
||||
|
||||
a(1) && b(1)
|
||||
a(0) && b(1)
|
||||
a(1) || b(1)
|
||||
a(0) || b(1)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
> a(1) && b(1);
|
||||
a: 1
|
||||
b: 1
|
||||
> a(0) && b(1);
|
||||
a: 0
|
||||
> a(1) || b(1);
|
||||
a: 1
|
||||
> a(0) || b(1);
|
||||
a: 0
|
||||
b: 1
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
SSEVAL1(IN)
|
||||
WRITE !,?10,$STACK($STACK,"PLACE")
|
||||
QUIT IN
|
||||
SSEVAL2(IN)
|
||||
WRITE !,?10,$STACK($STACK,"PLACE")
|
||||
QUIT IN
|
||||
SSEVAL3
|
||||
NEW Z
|
||||
WRITE "1 AND 1"
|
||||
SET Z=$$SSEVAL1(1) SET:Z Z=Z&$$SSEVAL2(1)
|
||||
WRITE !,$SELECT(Z:"TRUE",1:"FALSE")
|
||||
WRITE !!,"0 AND 1"
|
||||
SET Z=$$SSEVAL1(0) SET:Z Z=Z&$$SSEVAL2(1)
|
||||
WRITE !,$SELECT(Z:"TRUE",1:"FALSE")
|
||||
WRITE !!,"1 OR 1"
|
||||
SET Z=$$SSEVAL1(1) SET:'Z Z=Z!$$SSEVAL2(1)
|
||||
WRITE !,$SELECT(Z:"TRUE",1:"FALSE")
|
||||
WRITE !!,"0 OR 1"
|
||||
SET Z=$$SSEVAL1(0) SET:'Z Z=Z!$$SSEVAL2(1)
|
||||
WRITE !,$SELECT(Z:"TRUE",1:"FALSE")
|
||||
KILL Z
|
||||
QUIT
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
a := proc(bool)
|
||||
printf("a is called->%s\n", bool):
|
||||
return bool:
|
||||
end proc:
|
||||
b := proc(bool)
|
||||
printf("b is called->%s\n", bool):
|
||||
return bool:
|
||||
end proc:
|
||||
for i in [true, false] do
|
||||
for j in [true, false] do
|
||||
printf("calculating x := a(i) and b(j)\n"):
|
||||
x := a(i) and b(j):
|
||||
printf("calculating x := a(i) or b(j)\n"):
|
||||
y := a(i) or b(j):
|
||||
od:
|
||||
od:
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
a[in_] := (Print["a"]; in)
|
||||
b[in_] := (Print["b"]; in)
|
||||
a[False] && b[True]
|
||||
a[True] || b[False]
|
||||
|
|
@ -0,0 +1 @@
|
|||
a[True] && b[False]
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
MODULE ShortCircuit;
|
||||
FROM FormatString IMPORT FormatString;
|
||||
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
|
||||
|
||||
PROCEDURE a(v : BOOLEAN) : BOOLEAN;
|
||||
VAR buf : ARRAY[0..63] OF CHAR;
|
||||
BEGIN
|
||||
FormatString(" # Called function a(%b)\n", buf, v);
|
||||
WriteString(buf);
|
||||
RETURN v
|
||||
END a;
|
||||
|
||||
PROCEDURE b(v : BOOLEAN) : BOOLEAN;
|
||||
VAR buf : ARRAY[0..63] OF CHAR;
|
||||
BEGIN
|
||||
FormatString(" # Called function b(%b)\n", buf, v);
|
||||
WriteString(buf);
|
||||
RETURN v
|
||||
END b;
|
||||
|
||||
PROCEDURE Print(x,y : BOOLEAN);
|
||||
VAR buf : ARRAY[0..63] OF CHAR;
|
||||
VAR temp : BOOLEAN;
|
||||
BEGIN
|
||||
FormatString("a(%b) AND b(%b)\n", buf, x, y);
|
||||
WriteString(buf);
|
||||
temp := a(x) AND b(y);
|
||||
|
||||
FormatString("a(%b) OR b(%b)\n", buf, x, y);
|
||||
WriteString(buf);
|
||||
temp := a(x) OR b(y);
|
||||
|
||||
WriteLn;
|
||||
END Print;
|
||||
|
||||
BEGIN
|
||||
Print(FALSE,FALSE);
|
||||
Print(FALSE,TRUE);
|
||||
Print(TRUE,TRUE);
|
||||
Print(TRUE,FALSE);
|
||||
ReadChar
|
||||
END ShortCircuit.
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
def short_and(bool1, bool2)
|
||||
global a
|
||||
global b
|
||||
if a(bool1)
|
||||
if b(bool2)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def short_or(bool1, bool2)
|
||||
if a(bool1)
|
||||
return true
|
||||
else
|
||||
if b(bool2)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def a(bool)
|
||||
println "a called."
|
||||
return bool
|
||||
end
|
||||
|
||||
def b(bool)
|
||||
println "b called."
|
||||
return bool
|
||||
end
|
||||
|
||||
println "F and F = " + short_and(false, false) + "\n"
|
||||
println "F or F = " + short_or(false, false) + "\n"
|
||||
|
||||
println "F and T = " + short_and(false, true) + "\n"
|
||||
println "F or T = " + short_or(false, true) + "\n"
|
||||
|
||||
println "T and F = " + short_and(true, false) + "\n"
|
||||
println "T or F = " + short_or(true, false) + "\n"
|
||||
|
||||
println "T and T = " + short_and(true, true) + "\n"
|
||||
println "T or T = " + short_or(true, true) + "\n"
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
using System.Console;
|
||||
|
||||
class ShortCircuit
|
||||
{
|
||||
public static a(x : bool) : bool
|
||||
{
|
||||
WriteLine("a");
|
||||
x
|
||||
}
|
||||
|
||||
public static b(x : bool) : bool
|
||||
{
|
||||
WriteLine("b");
|
||||
x
|
||||
}
|
||||
|
||||
public static Main() : void
|
||||
{
|
||||
def t = true;
|
||||
def f = false;
|
||||
|
||||
WriteLine("True && True : {0}", a(t) && b(t));
|
||||
WriteLine("True && False: {0}", a(t) && b(f));
|
||||
WriteLine("False && True : {0}", a(f) && b(t));
|
||||
WriteLine("False && False: {0}", a(f) && b(f));
|
||||
WriteLine("True || True : {0}", a(t) || b(t));
|
||||
WriteLine("True || False: {0}", a(t) || b(f));
|
||||
WriteLine("False || True : {0}", a(f) || b(t));
|
||||
WriteLine("False || False: {0}", a(f) || b(f));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
a
|
||||
b
|
||||
True && True : True
|
||||
a
|
||||
b
|
||||
True && False: False
|
||||
a
|
||||
False && True : False
|
||||
a
|
||||
False && False: False
|
||||
a
|
||||
True || True : True
|
||||
a
|
||||
True || False: True
|
||||
a
|
||||
b
|
||||
False || True : True
|
||||
a
|
||||
b
|
||||
False || False: False
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
Parse Version v
|
||||
Say 'Version='v
|
||||
|
||||
If a() | b() Then Say 'a and b are true'
|
||||
If \a() | b() Then Say 'Surprise'
|
||||
Else Say 'ok'
|
||||
|
||||
If a(), b() Then Say 'a is true'
|
||||
If \a(), b() Then Say 'Surprise'
|
||||
Else Say 'ok: \\a() is false'
|
||||
|
||||
Select
|
||||
When \a(), b() Then Say 'Surprise'
|
||||
Otherwise Say 'ok: \\a() is false (Select)'
|
||||
End
|
||||
Return
|
||||
|
||||
method a private static binary returns boolean
|
||||
state = Boolean.TRUE.booleanValue()
|
||||
Say '--a returns' state
|
||||
Return state
|
||||
|
||||
method b private static binary returns boolean
|
||||
state = Boolean.TRUE.booleanValue()
|
||||
Say '--b returns' state
|
||||
Return state
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
proc a(x): bool =
|
||||
echo "a called"
|
||||
result = x
|
||||
|
||||
proc b(x): bool =
|
||||
echo "b called"
|
||||
result = x
|
||||
|
||||
let x = a(false) and b(true) # echoes "a called"
|
||||
let y = a(true) or b(true) # echoes "a called"
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
let a r = print_endline " > function a called"; r
|
||||
let b r = print_endline " > function b called"; r
|
||||
|
||||
let test_and b1 b2 =
|
||||
Printf.printf "# testing (%b && %b)\n" b1 b2;
|
||||
ignore (a b1 && b b2)
|
||||
|
||||
let test_or b1 b2 =
|
||||
Printf.printf "# testing (%b || %b)\n" b1 b2;
|
||||
ignore (a b1 || b b2)
|
||||
|
||||
let test_this test =
|
||||
test true true;
|
||||
test true false;
|
||||
test false true;
|
||||
test false false;
|
||||
;;
|
||||
|
||||
let () =
|
||||
print_endline "==== Testing and ====";
|
||||
test_this test_and;
|
||||
print_endline "==== Testing or ====";
|
||||
test_this test_or;
|
||||
;;
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
class ShortCircuit {
|
||||
function : a(a : Bool) ~ Bool {
|
||||
"a"->PrintLine();
|
||||
return a;
|
||||
}
|
||||
|
||||
function : b(b : Bool) ~ Bool {
|
||||
"b"->PrintLine();
|
||||
return b;
|
||||
}
|
||||
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
result := a(false) & b(false);
|
||||
"F and F = {$result}"->PrintLine();
|
||||
result := a(false) | b(false);
|
||||
"F or F = {$result}"->PrintLine();
|
||||
|
||||
result := a(false) & b(true);
|
||||
"F and T = {$result}"->PrintLine();
|
||||
result := a(false) | b(true);
|
||||
"F or T = {$result}"->PrintLine();
|
||||
|
||||
result := a(true) & b(false);
|
||||
"T and F = {$result}"->PrintLine();
|
||||
result := a(true) | b(false);
|
||||
"T or F = {$result}"->PrintLine();
|
||||
|
||||
result := a(true) & b(true);
|
||||
"T and T = {$result}"->PrintLine();
|
||||
result := a(true) | b(true);
|
||||
"T or T = {$result}"->PrintLine();
|
||||
}
|
||||
}
|
||||
25
Task/Short-circuit-evaluation/Ol/short-circuit-evaluation.ol
Normal file
25
Task/Short-circuit-evaluation/Ol/short-circuit-evaluation.ol
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
(define (a x)
|
||||
(print " (a) => " x)
|
||||
x)
|
||||
|
||||
(define (b x)
|
||||
(print " (b) => " x)
|
||||
x)
|
||||
|
||||
; and
|
||||
(print " -- and -- ")
|
||||
(for-each (lambda (x y)
|
||||
(print "let's evaluate '(a as " x ") and (b as " y ")':")
|
||||
(let ((out (and (a x) (b y))))
|
||||
(print " result is " out)))
|
||||
'(#t #t #f #f)
|
||||
'(#t #f #t #f))
|
||||
|
||||
; or
|
||||
(print " -- or -- ")
|
||||
(for-each (lambda (x y)
|
||||
(print "let's evaluate '(a as " x ") or (b as " y ")':")
|
||||
(let ((out (or (a x) (b y))))
|
||||
(print " result is " out)))
|
||||
'(#t #t #f #f)
|
||||
'(#t #f #t #f))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
Parse Version v
|
||||
Say 'Version='v
|
||||
If a() | b() Then Say 'a and b are true'
|
||||
If \a() | b() Then Say 'Surprise'
|
||||
Else Say 'ok'
|
||||
If a(), b() Then Say 'a is true'
|
||||
If \a(), b() Then Say 'Surprise'
|
||||
Else Say 'ok: \a() is false'
|
||||
Select
|
||||
When \a(), b() Then Say 'Surprise'
|
||||
Otherwise Say 'ok: \a() is false (Select)'
|
||||
End
|
||||
Exit
|
||||
a: Say 'a returns .true'; Return .true
|
||||
b: Say 'b returns 1'; Return 1
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
declare
|
||||
fun {A Answer}
|
||||
AnswerS = {Value.toVirtualString Answer 1 1}
|
||||
in
|
||||
{System.showInfo " % Called function {A "#AnswerS#"} -> "#AnswerS}
|
||||
Answer
|
||||
end
|
||||
|
||||
fun {B Answer}
|
||||
AnswerS = {Value.toVirtualString Answer 1 1}
|
||||
in
|
||||
{System.showInfo " % Called function {B "#AnswerS#"} -> "#AnswerS}
|
||||
Answer
|
||||
end
|
||||
in
|
||||
for I in [false true] do
|
||||
for J in [false true] do
|
||||
X Y
|
||||
in
|
||||
{System.showInfo "\nCalculating: X = {A I} andthen {B J}"}
|
||||
X = {A I} andthen {B J}
|
||||
{System.showInfo "Calculating: Y = {A I} orelse {B J}"}
|
||||
Y = {A I} orelse {B J}
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
Calculating: X = {A I} andthen {B J}
|
||||
% Called function {A false} -> false
|
||||
Calculating: Y = {A I} orelse {B J}
|
||||
% Called function {A false} -> false
|
||||
% Called function {B false} -> false
|
||||
|
||||
Calculating: X = {A I} andthen {B J}
|
||||
% Called function {A false} -> false
|
||||
Calculating: Y = {A I} orelse {B J}
|
||||
% Called function {A false} -> false
|
||||
% Called function {B true} -> true
|
||||
|
||||
Calculating: X = {A I} andthen {B J}
|
||||
% Called function {A true} -> true
|
||||
% Called function {B false} -> false
|
||||
Calculating: Y = {A I} orelse {B J}
|
||||
% Called function {A true} -> true
|
||||
|
||||
Calculating: X = {A I} andthen {B J}
|
||||
% Called function {A true} -> true
|
||||
% Called function {B true} -> true
|
||||
Calculating: Y = {A I} orelse {B J}
|
||||
% Called function {A true} -> true
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
a(n)={
|
||||
print(a"("n")");
|
||||
a
|
||||
};
|
||||
b(n)={
|
||||
print("b("n")");
|
||||
n
|
||||
};
|
||||
or(A,B)={
|
||||
a(A) || b(B)
|
||||
};
|
||||
and(A,B)={
|
||||
a(A) && b(B)
|
||||
};
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
short_circuit_evaluation:
|
||||
procedure options (main);
|
||||
declare (true initial ('1'b), false initial ('0'b) ) bit (1);
|
||||
declare (i, j, x, y) bit (1);
|
||||
|
||||
a: procedure (bv) returns (bit(1));
|
||||
declare bv bit(1);
|
||||
put ('Procedure ' || procedurename() || ' called.');
|
||||
return (bv);
|
||||
end a;
|
||||
b: procedure (bv) returns (bit(1));
|
||||
declare bv bit(1);
|
||||
put ('Procedure ' || procedurename() || ' called.');
|
||||
return (bv);
|
||||
end b;
|
||||
|
||||
do i = true, false;
|
||||
do j = true, false;
|
||||
put skip(2) list ('Evaluating x with <a> with ' || i || ' and <b> with ' || j);
|
||||
put skip;
|
||||
if a(i) then
|
||||
x = b(j);
|
||||
else
|
||||
x = false;
|
||||
put skip data (x);
|
||||
put skip(2) list ('Evaluating y with <a> with ' || i || ' and <b> with ' || j);
|
||||
put skip;
|
||||
if a(i) then
|
||||
y = true;
|
||||
else
|
||||
y = b(j);
|
||||
put skip data (y);
|
||||
end;
|
||||
end;
|
||||
end short_circuit_evaluation;
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
program shortcircuit(output);
|
||||
|
||||
function a(value: boolean): boolean;
|
||||
begin
|
||||
writeln('a(', value, ')');
|
||||
a := value
|
||||
end;
|
||||
|
||||
function b(value:boolean): boolean;
|
||||
begin
|
||||
writeln('b(', value, ')');
|
||||
b := value
|
||||
end;
|
||||
|
||||
procedure scandor(value1, value2: boolean);
|
||||
var
|
||||
result: boolean;
|
||||
begin
|
||||
{and}
|
||||
if a(value1)
|
||||
then
|
||||
result := b(value2)
|
||||
else
|
||||
result := false;
|
||||
writeln(value1, ' and ', value2, ' = ', result);
|
||||
|
||||
{or}
|
||||
if a(value1)
|
||||
then
|
||||
result := true
|
||||
else
|
||||
result := b(value2)
|
||||
writeln(value1, ' or ', value2, ' = ', result);
|
||||
end;
|
||||
|
||||
begin
|
||||
scandor(false, false);
|
||||
scandor(false, true);
|
||||
scandor(true, false);
|
||||
scandor(true, true);
|
||||
end.
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
program shortcircuit;
|
||||
|
||||
function a(value: boolean): boolean;
|
||||
begin
|
||||
writeln('a(', value, ')');
|
||||
a := value;
|
||||
end;
|
||||
|
||||
function b(value:boolean): boolean;
|
||||
begin
|
||||
writeln('b(', value, ')');
|
||||
b := value;
|
||||
end;
|
||||
|
||||
{$B-} {enable short circuit evaluation}
|
||||
procedure scandor(value1, value2: boolean);
|
||||
var
|
||||
result: boolean;
|
||||
begin
|
||||
result := a(value1) and b(value);
|
||||
writeln(value1, ' and ', value2, ' = ', result);
|
||||
|
||||
result := a(value1) or b(value2);
|
||||
writeln(value1, ' or ', value2, ' = ', result);
|
||||
end;
|
||||
|
||||
begin
|
||||
scandor(false, false);
|
||||
scandor(false, true);
|
||||
scandor(true, false);
|
||||
scandor(true, true);
|
||||
end.
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
program shortcircuit(output);
|
||||
|
||||
function a(value: boolean): boolean;
|
||||
begin
|
||||
writeln('a(', value, ')');
|
||||
a := value
|
||||
end;
|
||||
|
||||
function b(value:boolean): boolean;
|
||||
begin
|
||||
writeln('b(', value, ')');
|
||||
b := value
|
||||
end;
|
||||
|
||||
procedure scandor(value1, value2: boolean);
|
||||
var
|
||||
result: integer;
|
||||
begin
|
||||
result := a(value1) and_then b(value)
|
||||
writeln(value1, ' and ', value2, ' = ', result);
|
||||
|
||||
result := a(value1) or_else b(value2);
|
||||
writeln(value1, ' or ', value2, ' = ', result)
|
||||
end;
|
||||
|
||||
begin
|
||||
scandor(false, false);
|
||||
scandor(false, true);
|
||||
scandor(true, false);
|
||||
scandor(true, true);
|
||||
end.
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
sub a { print 'A'; return $_[0] }
|
||||
sub b { print 'B'; return $_[0] }
|
||||
|
||||
# Test-driver
|
||||
sub test {
|
||||
for my $op ('&&','||') {
|
||||
for (qw(1,1 1,0 0,1 0,0)) {
|
||||
my ($x,$y) = /(.),(.)/;
|
||||
print my $str = "a($x) $op b($y)", ': ';
|
||||
eval $str; print "\n"; } }
|
||||
}
|
||||
|
||||
# Test and display
|
||||
test();
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a "</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"b "</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">z</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a(%d) and b(%d) "</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" => %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a(%d) or b(%d) "</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" => %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
(de a (F)
|
||||
(msg 'a)
|
||||
F )
|
||||
|
||||
(de b (F)
|
||||
(msg 'b)
|
||||
F )
|
||||
|
||||
(mapc
|
||||
'((I J)
|
||||
(for Op '(and or)
|
||||
(println I Op J '-> (Op (a I) (b J))) ) )
|
||||
'(NIL NIL T T)
|
||||
'(NIL T NIL T) )
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
int(0..1) a(int(0..1) i)
|
||||
{
|
||||
write(" a\n");
|
||||
return i;
|
||||
}
|
||||
|
||||
int(0..1) b(int(0..1) i)
|
||||
{
|
||||
write(" b\n");
|
||||
return i;
|
||||
}
|
||||
|
||||
foreach(({ ({ false, false }), ({ false, true }), ({ true, true }), ({ true, false }) });; array(int) args)
|
||||
{
|
||||
write(" %d && %d\n", @args);
|
||||
a(args[0]) && b(args[1]);
|
||||
|
||||
write(" %d || %d\n", @args);
|
||||
a(args[0]) || b(args[1]);
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
# Simulated fast function
|
||||
function a ( [boolean]$J ) { return $J }
|
||||
|
||||
# Simulated slow function
|
||||
function b ( [boolean]$J ) { Sleep -Seconds 2; return $J }
|
||||
|
||||
# These all short-circuit and do not evaluate the right hand function
|
||||
( a $True ) -or ( b $False )
|
||||
( a $True ) -or ( b $True )
|
||||
( a $False ) -and ( b $False )
|
||||
( a $False ) -and ( b $True )
|
||||
|
||||
# Measure of execution time
|
||||
Measure-Command {
|
||||
( a $True ) -or ( b $False )
|
||||
( a $True ) -or ( b $True )
|
||||
( a $False ) -and ( b $False )
|
||||
( a $False ) -and ( b $True )
|
||||
} | Select TotalMilliseconds
|
||||
|
||||
# These all appropriately do evaluate the right hand function
|
||||
( a $False ) -or ( b $False )
|
||||
( a $False ) -or ( b $True )
|
||||
( a $True ) -and ( b $False )
|
||||
( a $True ) -and ( b $True )
|
||||
|
||||
# Measure of execution time
|
||||
Measure-Command {
|
||||
( a $False ) -or ( b $False )
|
||||
( a $False ) -or ( b $True )
|
||||
( a $True ) -and ( b $False )
|
||||
( a $True ) -and ( b $True )
|
||||
} | Select TotalMilliseconds
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
short_circuit :-
|
||||
( a_or_b(true, true) -> writeln('==> true'); writeln('==> false')) , nl,
|
||||
( a_or_b(true, false)-> writeln('==> true'); writeln('==> false')) , nl,
|
||||
( a_or_b(false, true)-> writeln('==> true'); writeln('==> false')) , nl,
|
||||
( a_or_b(false, false)-> writeln('==> true'); writeln('==> false')) , nl,
|
||||
( a_and_b(true, true)-> writeln('==> true'); writeln('==> false')) , nl,
|
||||
( a_and_b(true, false)-> writeln('==> true'); writeln('==> false')) , nl,
|
||||
( a_and_b(false, true)-> writeln('==> true'); writeln('==> false')) , nl,
|
||||
( a_and_b(false, false)-> writeln('==> true'); writeln('==> false')) .
|
||||
|
||||
|
||||
a_and_b(X, Y) :-
|
||||
format('a(~w) and b(~w)~n', [X, Y]),
|
||||
( a(X), b(Y)).
|
||||
|
||||
a_or_b(X, Y) :-
|
||||
format('a(~w) or b(~w)~n', [X, Y]),
|
||||
( a(X); b(Y)).
|
||||
|
||||
a(X) :-
|
||||
format('a(~w)~n', [X]),
|
||||
X.
|
||||
|
||||
b(X) :-
|
||||
format('b(~w)~n', [X]),
|
||||
X.
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
?- short_circuit.
|
||||
a(true) or b(true)
|
||||
a(true)
|
||||
==> true
|
||||
|
||||
a(true) or b(false)
|
||||
a(true)
|
||||
==> true
|
||||
|
||||
a(false) or b(true)
|
||||
a(false)
|
||||
b(true)
|
||||
==> true
|
||||
|
||||
a(false) or b(false)
|
||||
a(false)
|
||||
b(false)
|
||||
==> false
|
||||
|
||||
a(true) and b(true)
|
||||
a(true)
|
||||
b(true)
|
||||
==> true
|
||||
|
||||
a(true) and b(false)
|
||||
a(true)
|
||||
b(false)
|
||||
==> false
|
||||
|
||||
a(false) and b(true)
|
||||
a(false)
|
||||
==> false
|
||||
|
||||
a(false) and b(false)
|
||||
a(false)
|
||||
==> false
|
||||
|
||||
true.
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
Procedure a(arg)
|
||||
PrintN(" # Called function a("+Str(arg)+")")
|
||||
ProcedureReturn arg
|
||||
EndProcedure
|
||||
|
||||
Procedure b(arg)
|
||||
PrintN(" # Called function b("+Str(arg)+")")
|
||||
ProcedureReturn arg
|
||||
EndProcedure
|
||||
|
||||
OpenConsole()
|
||||
For a=#False To #True
|
||||
For b=#False To #True
|
||||
PrintN(#CRLF$+"Calculating: x = a("+Str(a)+") And b("+Str(b)+")")
|
||||
x= a(a) And b(b)
|
||||
PrintN("Calculating: x = a("+Str(a)+") Or b("+Str(b)+")")
|
||||
y= a(a) Or b(b)
|
||||
Next
|
||||
Next
|
||||
Input()
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
>>> def a(answer):
|
||||
print(" # Called function a(%r) -> %r" % (answer, answer))
|
||||
return answer
|
||||
|
||||
>>> def b(answer):
|
||||
print(" # Called function b(%r) -> %r" % (answer, answer))
|
||||
return answer
|
||||
|
||||
>>> for i in (False, True):
|
||||
for j in (False, True):
|
||||
print ("\nCalculating: x = a(i) and b(j)")
|
||||
x = a(i) and b(j)
|
||||
print ("Calculating: y = a(i) or b(j)")
|
||||
y = a(i) or b(j)
|
||||
|
||||
|
||||
|
||||
Calculating: x = a(i) and b(j)
|
||||
# Called function a(False) -> False
|
||||
Calculating: y = a(i) or b(j)
|
||||
# Called function a(False) -> False
|
||||
# Called function b(False) -> False
|
||||
|
||||
Calculating: x = a(i) and b(j)
|
||||
# Called function a(False) -> False
|
||||
Calculating: y = a(i) or b(j)
|
||||
# Called function a(False) -> False
|
||||
# Called function b(True) -> True
|
||||
|
||||
Calculating: x = a(i) and b(j)
|
||||
# Called function a(True) -> True
|
||||
# Called function b(False) -> False
|
||||
Calculating: y = a(i) or b(j)
|
||||
# Called function a(True) -> True
|
||||
|
||||
Calculating: x = a(i) and b(j)
|
||||
# Called function a(True) -> True
|
||||
# Called function b(True) -> True
|
||||
Calculating: y = a(i) or b(j)
|
||||
# Called function a(True) -> True
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
>>> for i in (False, True):
|
||||
for j in (False, True):
|
||||
print ("\nCalculating: x = a(i) and b(j) using x = b(j) if a(i) else False")
|
||||
x = b(j) if a(i) else False
|
||||
print ("Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True")
|
||||
y = b(j) if not a(i) else True
|
||||
|
||||
|
||||
|
||||
Calculating: x = a(i) and b(j) using x = b(j) if a(i) else False
|
||||
# Called function a(False) -> False
|
||||
Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True
|
||||
# Called function a(False) -> False
|
||||
# Called function b(False) -> False
|
||||
|
||||
Calculating: x = a(i) and b(j) using x = b(j) if a(i) else False
|
||||
# Called function a(False) -> False
|
||||
Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True
|
||||
# Called function a(False) -> False
|
||||
# Called function b(True) -> True
|
||||
|
||||
Calculating: x = a(i) and b(j) using x = b(j) if a(i) else False
|
||||
# Called function a(True) -> True
|
||||
# Called function b(False) -> False
|
||||
Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True
|
||||
# Called function a(True) -> True
|
||||
|
||||
Calculating: x = a(i) and b(j) using x = b(j) if a(i) else False
|
||||
# Called function a(True) -> True
|
||||
# Called function b(True) -> True
|
||||
Calculating: y = a(i) or b(j) using y = b(j) if not a(i) else True
|
||||
# Called function a(True) -> True
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
[ 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
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
a <- function(x) {cat("a called\n"); x}
|
||||
b <- function(x) {cat("b called\n"); x}
|
||||
|
||||
tests <- expand.grid(op=list(quote(`||`), quote(`&&`)), x=c(1,0), y=c(1,0))
|
||||
|
||||
invisible(apply(tests, 1, function(row) {
|
||||
call <- substitute(op(a(x),b(y)), row)
|
||||
cat(deparse(call), "->", eval(call), "\n\n")
|
||||
}))
|
||||
27
Task/Short-circuit-evaluation/R/short-circuit-evaluation-2.r
Normal file
27
Task/Short-circuit-evaluation/R/short-circuit-evaluation-2.r
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
a called
|
||||
a(1) || b(1) -> TRUE
|
||||
|
||||
a called
|
||||
b called
|
||||
a(1) && b(1) -> TRUE
|
||||
|
||||
a called
|
||||
b called
|
||||
a(0) || b(1) -> TRUE
|
||||
|
||||
a called
|
||||
a(0) && b(1) -> FALSE
|
||||
|
||||
a called
|
||||
a(1) || b(0) -> TRUE
|
||||
|
||||
a called
|
||||
b called
|
||||
a(1) && b(0) -> FALSE
|
||||
|
||||
a called
|
||||
b called
|
||||
a(0) || b(0) -> FALSE
|
||||
|
||||
a called
|
||||
a(0) && b(0) -> FALSE
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue