Ch.6 Introduction to ML programming

§ 6.15. Procedural control

We have learned imperative (procedural) programming. At this point, let us review the relation between control statements in a procedural language and functions.

Inherently procedural operations such as external IO are naturally expressed as procedural programming. However, in a procedural language, control structures such as loops and conditionals are also implemented using states. Since program structures are independent of imperative operation, representing them as expressions generally yield more declarative programs.

There is one thing to be noted however. In a procedural language like C, the factorial function may be written as follows.

  int fact (int n) {
    int s = 1;
    while (n > 0) {
      s = s * n;
      n = n - 1;
    return s;

This is generally more efficient than a recursive function of the form

  fun fact 0 = 1
       | fact n = n * fact (n - 1)

This fact is however does not implies that functional programming is inherently inefficient.