Programming Language--Section1

Conditional expression

  • Syntax
if e1 then e2 else e3

Where if, then, and else are keywords and e1, e2 and e3 are subexpressions.

  • Type-checking:
    First e1 must have type bool, e2 and e3 can have any type (let's call it t), but they must have the same type t. The type of the entire expression is also t.

  • Evaluation rules:
    Firsgt evaluate e1 to a value call it v1, if it is true, evaluate e2 and that result is the whole expression. else, evaluate e3 and that result is the whole expression's result.

  • My own example:

if 8 < 10 then "Hello!" else "So bad."

Then the return of which is

val it = "Hello!" : string























你可能感兴趣的:(Programming Language--Section1)