Finally, let me illustrate the way you use references by showing how to build a fixed-point combinator using references. Here you see what we do is exploit the fact that we can create a reference cell of type integer and our fixed-point operator what will it do? Well, it will assign to this reference Z, a function of this form integer arrow integer. Okay, we will end this summary lecture by illustrating how to achieve the definition of a factorial in yet two more ways using references. So, look at this. We create a reference cell whose type is int arrow int. We define originally the function fact 3 this time as a function which if N=0 then returns 1 otherwise it multiplies N by what is the function that is stored in this reference applied to N-1. Now what happens if we store precisely the function itself? Well, in this case what we get is again recursion. This is clear proof that if we have references then we have non-terminating expressions even if we work in a simply typed version as that of Standard ML. A more general solution is given here where we actually build the operator Y. As you can see the trick is again the same. We create a reference of type int arrow int and we define the function Y to be essentially the function which accesses what is stored in this cell after having applied precisely the function F to the contents of the cell. So for the last time let's see what the factorial will look like. Well, it looks exactly as all the other factorials. It is the operator Y which has changed. Of course, during the course we have devoted a lot of attention also to modules, to other examples. We have discussed in class various examples and there have also been some practical sessions. | ![]() |