Matti's CS Notebook

1.1 Elements of programming

1 (a) The given expression 10; evaluates to 10; (b) Since Javascript’s + operator is left associative, the expression 5 + 5 + 4; is evaluated as (5 + 5) + 4 $=$ 10 + 4 $=$ 14. (c) The expression 9 - 1; evaluates to 8. (d) Expression 6 / 2; evaluates to 3. (e) Due to the parentheses having greater priority in the order of eveluations in Javascript, the expression 2 * 4 + (4 - 6); first evaluates to `` 2 * 4 + (-2), then to 2 * 4 - 2, then to (2 * 4) - 2, then to 8 - 2 and finally to 6. (d) Expression const a = 3 evaluates to undefined, because this expression is a declaration, but the expression a; evaluates to 3. (f) The expression const b = a + 1; is evaluated as const b = (a + 1); and evaluates to undefined too, because this expression is also a declaration, but the expression b; evaluates to 4, because b is equal to a + 1 = (3 + 1) that is equal to 4. (g) Expression a + b + a * b; is evaluated as ((a + b) + (a * b)), where the second term (a * b) is evaluated first, then the first term (a + b) and then the sum of these two terms. (h) Expression a === b is evaluated as (a === b). (i) According to the operator precedence of Javascript expression b > a && b < a * b ? b : a is evaluated as

    b > a && a < b * b ? b : a
    = ((b > a) && (a < (b * (b ? b : a)))).

(i) Expression a === 4; is evaluated into a error if a is not defined or either to true or false is a is a number. (j) Expression ? 6; evaluates to error because there is no unary, binary nor ternary operations ? in Javascript. (k) Expression : b === 4; could first evaluated as : (b === 4), but since the left handside of the ?: operator is missing and operand, the whole evaluates to an error. (l) Same goes for the expression ? 6 + 7 + a;, the left hand-side operand is missing, so the expression leads to an evaluation error. (m) Expression 2 + (b > a ? b : a); is evaluated as

    (2 + (b > (a ? b : a))).

(n) The expression

    (a > b
     ? a
     : a < b
     ? b
     : -1
    )
    *
    (a + 1);

can be written as

    (a > b ? a : a < b ? b : -1) * (a + 1);

and is evaluated as

    (((a > b) ? a : ((a < b) ? b : -1)) * (a + 1)).

2 The given expression

$$ \frac{(5 + 4 + (2 - (3 - (6 + \frac{4}{5}))))}{3(6 - 2)(2 - 7)} $$

can be translated into Javascript by writing:

    (5 + 4 + (2 - (3 - (6 + (4 / 5)))))
        / 3 * (6 - 2) * (2 - 7)

3

4 If

function plus(a,b)
{
    return a + b;
}

and

function minus(a,b)
{
    return a - b;
}

then the function

function a_plus_abs_b(a,b)
{
    return (b >= 0 ? plus : minus)(a,b);
}

is evaluated by first checking the condition (b >= 0). On true or false, either plus or minus is returned, that is, the grouping (b >= 0 ? plus : minus) is resolved. This results in a function call to plus or minus to which the set of arguments group (a,b) are used. The first group is grouping resolution and the second a function call of plus or minus.

5 If Ben Bitdiddle uses the interpreter with applicative-order evaluation the function call

test(0,p());

where p is defined as

function p() { return p(); }

and test as

function test(x, y)
{
    return x === 0 ? 0 : y;
}

Bitdiddle will see the “evaluate the arguments and then apply” behaviour where the function call test() evaluates to RangeError, because the intepreter will first evaluate 0 as zero, and then interpreter evaluates p() that by definition is a infinite recursion.

However if the interpreter uses “normal-order evaluation”, the call p() means that the function will be “fully expanded and then reduced”, which means that the call test(0,p()) is evaluated as

    0 === (0 ? 0 : p())

which leads to

    0 === p()

which also yields RangeError.