undefined
at the beginning of function. (This is JavaScript var
.)
var x = 3; // outer x scope
function f() { // inner x scope, begins with x undefined
print(x); // undefined
var x = x + 2; // x assigned undefined+2 i.e. NaN
print(x); // NaN
}
f();
let
, illustrating the temporal dead zone.)
var x = 3; // outer x scope
function f() { // inner x scope, TDZ begins
print(x); // Access in TDZ: Runtime error
var x = x + 2; // x assigned undefined+2 i.e. NaN
print(x); // Execution never gets here
}
f();
var x = 3; // outer x scope
function f() { // inner x scope begins with no init, just garbage
print(x); // garbage
var x = x + 2; // x assigned whatever garbage it started with + 2
print(x); // garbage+2
}
f();
var x = 3; // outer x scope
function f() {
print(x); // still using outer x, prints 3
var x = x + 2; // inner x scope
print(x); // garbage+2
}
f();
var x = 3;
function f() {
print(x);
var x = x + 2; // compiler rejects usage of x here
print(x);
}
f();
g
were a function which modified the global variable a
as a side effect. Then the expression f(a,g(b))
could yield different results depending on which order the arguments were evaluated. (I am hoping your answer shows an exact case!)function includesAsField(structType, type) { // Whether the struct type has a field of type type, directly or indirectly return structType.fields.some( field => field.type === type || (field.type?.kind === "StructType" && includesAsField(field.type, type)) ) } function mustNotBeSelfContaining(structType, at) { const containsSelf = includesAsField(structType, structType) must(!containsSelf, "Struct type must not be self-containing", at) }
function min_element(a) if len(a) == 1 then return a[0] else return min(a[0], min_element(a[1..])) end end
However, this is not tail recursive, and is not an acceptable solution. To make it tail recursive you need the trick where your recursive function accepts a list and the current minimum so far. Here are some solutions to the tail-recursive part. In most cases these would need to be wrapped in a function which did not take in the “accumulator” min-so-far variable.
def minimum(a, min_so_far = math.inf): if not a: return min_so_far return minimum(a[1:], min(a[0], min_so_far))
double minimum(double *array, int len, double min_so_far) { if (len == 0) { return min_so_far; } double new_min = array[0] < min_so_far ? array[0] : min_so_far; return minimum(array+1, len-1, new_min); }
function minimum(array, minSoFar = Infinity) { if (array.length === 0) return minSoFar return minimum(array.slice(1), Math.min(a[0], minSoFar)) }
// Thanks to ChatGPT for this func minimum(array []float64, minSoFar float64) float64 { if len(array) == 0 { return minSoFar } else { newMin := minSoFar if minSoFar < array[0] { newMin = array[0] } return minimum(array[1:], newMin) } }
minimum([], Min) -> Min; minimum([Head | Tail], Min) -> minimum(Tail, min(Head, Min)).
fn minimum(arr: &[f64], min_so_far: f64) -> f64 { match arr { [] => min_so_far, [head, tail @ ..] => minimum(tail, if *head < min_so_far { *head } else { min_so_far }) } }
undefined
to the callback of setTimeout
. This is an example where delayed execution is called for, so to fix it, you need to replace update(i)
with ()=>update(i)
. There’s another way, too. You can invoke setTimeout
with a third argument: setTimeout(1000, update, i)
. That’s a bit harder to read, but it does work!zero
.FindBugs and FindSecBugs might find more!