I realise that this could raise (ahem) "copyright issues", so if anyone at the University with any authority in this matter sees this, I would appreciate it if you would mail me with your views or suggestions.
Thanks.
Since this assignment I have found that I can get hold of these papers for a mere forty pence per paper, so it's not as pressing an issue any more. It is useful to have these papers online I think though, so I plan to carry on anyway at some point, in my (as a friend would say) copious free time.
Describe the different parts of a signature and explain how arguments may be supplied when a procedure is invoked.
MODULE Main; IMPORT Fmt, IO; EXCEPTION Bad; VAR j: CARDINAL := 0; PROCEDURE P (i: CARDINAL): CARDINAL RAISES {Bad} = BEGIN IF i = 0 THEN RAISE Bad END; RETURN j + 100 DIV i; END P; PROCEDURE Q (i: CARDINAL): CARDINAL RAISES {Bad} = BEGIN TRY RETURN P (i); FINALLY j := j + i + 1; END; END Q; VAR k: CARDINAL := 0; BEGIN TRY k := Q (1); k := k + Q (0); (**) EXCEPT Bad => IO.Put ("Bad Raised\n"); END; IO.Put ("k = " & Fmt.Int (k) & "\n" & "j = " & Fmt.Int (j) & "\n"); END Main.Rewrite procedures P, Q and the main program so that they perform the same calculations and have the same side-effects without using the exceptions.
If the line marked (**) is replaced by the line:
k := Q (1) + Q (0);what can you say about the output from the program?
It is sometimes useful to achieve an effect analogous to having an array that can be indexed using values of type TEXT. One way of doing this is to use a structure known as a hash table: given an index of type TEXT an integer is compared using a hash function and this is then used to index an array. The library procedure Text.Hash computes a suitable integer from a TEXT value. Two complications arise. First the integer computed by the hash function may lie outside the valid range of index values for the array. Secondly two different TEXT objects may give rise to the same hash value.
The problems can be resolved first by reducing the raw hash value modulo the size of the array and arranging that each array entry refers to the start of a linked list of (index, value) pairs. Retrieving a value from the table involves accessing the array to obtain the correct list of pairs and then scanning the list to find an index vale that is identical (use the library function Text.Equal) with the TEXT index being sought. The corresponding value can then be returned. Storing into the table will involve adding a new (index, value) pair to one of the lists.
Design appropriate data structures for such a table, and write procedures to store and retrieve values, using the following signatures:
PROCEDURE Put(VAR table: Table; key, value: TEXT) RAISES {DuplicateKey} PROCEDURE Get(READONLY table: Table; key: TEXT): TEXT RAISES {MissingKey}
TYPE List = REF Link; Link = RECORD value: CARDINAL; rest: List:= NIL END;A test program which exploits lists of this kind includes:
VAR start: List; BEGIN start:= NIL; Put (10,start); Put (100,start); Put (1000,start); Print (start); Print (Reverse1 (start)); Print (Reverse2 (start));The procedure call Put (1000,start) will add a link containing the value 1000 to the end of the list which already includes the values 10 and 100.
The procedure Print writes out the values in a list in order.
The procedure Reverse1 and Reverse2 reverse a list in two different ways, equivalent to the ML functions:
fun Reverse1 [] = [] | Reverse1 (value::rest) = Reverse1(rest) @ [value]; fun Reverse2 list = let fun rev ([], result) = result | rev(value::rest, result) = rev(rest, value::result) in rev(list, []) end;Write the Modula-3 procedures Put, Print, Reverse1 and Reverse2.
VAR r := Create("3.14159"); VAR r := Create("42"); VAR s := r.add(i); VAR j := i.add(Create("37"));The sub-type associated with the values of i, j will be Integer, whereas that associated with the values of r, s will be Real.
Sketch the procedure to create a Number of appropriate type from an
argument text string.
[Recall the library routines Scan.Int and
Scan.Real which may raise the exception
Scan.BadFormat.]
Sketch also the procedures that implement the add and text methods for Integer and Real. If an Integer and a Real are to be added, the value associated with the Integer should automatically be converted to a Real.