04 Application

Function application is written just by putting functions next to each other

e0 e1 ... en, where e0 is the function expression, and e1 through en are the parameters. Note that there are no commas or parenthesis, it's incorrect to add them.

Evaluation

Evaluation is done by first evaluating all of the sub-expressions (e1 through en) then evaluating the function.

  1. e0 => v0, ..., en => vn where v0 ... vn are functions with signature fun x1 ... xn -> e
  2. Substitute vi for xi yielding the new expression e'. Evaluate it e' ==> v
  3. Result is v.

Let's try it with the function fun x -> x + 1

What's happening here?

  1. e0 is evaluated to a value - done because the anonymous function is a value.
  2. e1 is evaluated to a value - 2 + 3 ==> 5. Now we have (fun x -> x + 1) 5;;.
  3. Substitute the parameters for their values: 5 + 1.
  4. Evaluate 5 + 1 ==> 6.

Now let's try another example:

  1. e0 is evaluated to a value – done because the anonymous function is a value.
  2. e1 and e2 are evaluated to values
    • 3 * 1 ==> 3
    • 3 - 1 ==> 2
  3. The parameters are substituted for their values
    • 3 + 2
  4. The expression is evaluated
    • 3 + 2 ==> 5