We may use if then else in ML however pattern matching is preferred. The function past does this. The final equation has the free variable x as the formal parameter - this will match with any string not caught by the previous equations. In the following examples we use exactly two patterns for our functions. Recursion Using recursive functions we can achieve the sort of results which would require loops in a traditional language.
Recursive functions tend to be much shorter and clearer.
A recursive function is one which calls itself either directly or indirectly. Traditionally, the first recursive function considered is factorial. To see how this works consider the execution of factorial 3 . This generates a further call to factorial before the multiplication can take place. In evaluating factorial 2 the second equation is used but this time n is bound to 2 .