2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,18 +1,28 @@
{{Control Structures}}
Assume functions <math>a</math> and <math>b</math> return boolean values, and further, the execution of function <math>b</math> takes considerable resources without side effects, <!--treating the printing as being for illustrative purposes only--> and is to be minimised.
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 <math>b()</math> if the value of <math>a()</math> is computed as <math>\mathrm{false}</math>, as the value of <math>x</math> can then only ever be <math>\mathrm{false}</math>.
Assume functions &nbsp; <code>a</code> &nbsp; and &nbsp; <code>b</code> &nbsp; return boolean values, &nbsp; and further, the execution of function &nbsp; <code>b</code> &nbsp; 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 &nbsp; (<code>and</code>):
:::: <code> x = a() and b() </code>
Then it would be best to not compute the value of &nbsp; <code>b()</code> &nbsp; if the value of &nbsp; <code>a()</code> &nbsp; is computed as &nbsp; <code>false</code>, &nbsp; as the value of &nbsp; <code>x</code> &nbsp; can then only ever be &nbsp; <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 <math>b()</math> if the value of <math>a()</math> is computed as <math>\mathrm{true}</math>, as the value of <math>y</math> can then only ever be <math>\mathrm{true}</math>.
:::: <code> y = a() or b() </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
Then it would be best to not compute the value of &nbsp; <code>b()</code> &nbsp; if the value of &nbsp; <code>a()</code> &nbsp; is computed as &nbsp; <code>true</code>, &nbsp; as the value of &nbsp; <code>y</code> &nbsp; can then only ever be &nbsp; <code>true</code>.
;Task Description
The task is to create two functions named <math>a</math> and <math>b</math>, 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 <math>b</math> is only called when necessary:
:<code>x = a(i) and b(j)</code>
:<code>y = a(i) or b(j)</code>
If the language does not have short-circuit evaluation, this might be achieved with nested <code>if</code> statements.
Some languages will stop further computation of boolean equations as soon as the result is known, so-called &nbsp; [[wp:Short-circuit evaluation|short-circuit evaluation]] &nbsp; of boolean expressions
;Task:
Create two functions named &nbsp; <code>a</code> &nbsp; and &nbsp; <code>b</code>, &nbsp; 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 &nbsp; <code>b</code> &nbsp; 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 &nbsp; &nbsp; '''if''' &nbsp; &nbsp; statements.
<br><br>

View file

@ -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

View file

@ -0,0 +1,22 @@
#import system.
#import system'routines.
#import extensions.
#symbol a = x [ console writeLine:"a". ^ x. ].
#symbol b = x [ console writeLine:"b". ^ x. ].
#symbol program =
[
(false, true) run &each: i
[
(false, true) run &each: j
[
console writeLine:i:" and ":j:" = ":(a eval:i and:[ b eval:j ]).
console writeLine.
console writeLine:i:" or ":j:" = ":(a eval:i or:[ b eval:j ]).
console writeLine.
].
].
].

View 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
)
)

View file

@ -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];
})();

View file

@ -1,11 +1,13 @@
use MONKEY-SEE-NO-EVAL;
sub a ($p) { print 'a'; $p }
sub b ($p) { print 'b'; $p }
for '&&', '||' -> $op {
for True, False X True, False -> $p, $q {
for 1, 0 X 1, 0 -> ($p, $q) {
for '&&', '||' -> $op {
my $s = "a($p) $op b($q)";
print "$s: ";
eval $s;
EVAL $s;
print "\n";
}
}

View file

@ -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

View file

@ -1,12 +1,16 @@
/*REXX programs demonstrates short-circuit evaulation testing. */
/*REXX programs demonstrates short-circuit evaluation testing (in an IF statement).*/
parse arg LO HI . /*obtain optional arguments from he CL.*/
if LO=='' | LO=="," then LO= -2 /*Not specified? Then use the default.*/
if HI=='' | HI=="," then HI= 2 /* " " " " " " */
do i=-2 to 2
x=a(i) & b(i)
y=a(i)
if \y then y=b(i)
say copies('',30) 'x='||x 'y='y 'i='i
end /*j*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────subroutines─────────────────────────*/
a: say 'A entered with:' arg(1);return abs(arg(1)//2) /*1=odd, 0=even */
b: say 'B entered with:' arg(1);return arg(1)<0 /*1=neg, 0=if not*/
do j=LO to HI /*process from the low to the high.*/
x=a(j) & b(j) /*compute function A and function B */
y=a(j) | b(j) /* " " " or " " */
if \y then y=b(j) /* " " B (for negation).*/
say copies('', 30) ' x=' || x ' y='y ' j='j
say
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
a: say ' A entered with:' arg(1); return abs( arg(1) // 2) /*1=odd, 0=even */
b: say ' B entered with:' arg(1); return arg(1) < 0 /*1=neg, 0=if not*/