Perfect Developer basic tutorial 2 This page last modified 2011-10-29 (JAC)

Brackets and Conditional Expressions Quiz

1. What does the expression ([x > y]: x, []: y) compute, given that x and y are of the same type and that the ~~ operator defines a total ordering on that type?

The maximum of x and y.

It can be read as "if x greater than y then x, else y". If the ~~ operator does not define a total ordering, it will still return a "maximum" in the sense that the result is not less than either x or y.


2. Use a let-declaration to simplify the following expression:

  ( [([a < b]: b - a, []: a - b) > 10]:
      "The difference is " ++ ([a < b]: b - a, []: a - b).tostring ++ " which is too large",
    []:
      "OK"
  )

(in case you are wondering, the ++ operator performs string concatenation in this context).

  ( let diff ^= ([a < b]: b - a, []: a - b);
    [diff > 10]:
      "The diference is " ++ diff.tostring ++ " which is too large",
    []:
      "OK"
  )