PPP Exercises: Functions and Orthogonality
Exercise: Functions & parameters
Write functions that find the maximum
- of two integers;
- of three integers;
- of a tuple with two integer fields;
- of an array of integers.
Exercise: Integer/String pairs
- Define a type 'IntString' for pairs consisting of two fields, one of type 'Int', the other of type 'String', named 'fst' and 'snd'.
- Define a function 'new' that uses an integer and a string as parameters and returns a pair of type 'IntString'.
- Define functions 'getFst' and 'getSnd' that return the first/second value of such a pair.
- Define a function 'addConcat' that takes two pairs and returns a new pair whose integer component is the sum of the integer components of its parameters and whose string component is the the concatenation of the string components of its parameters. Note: The string concatenation operator in TL is '<>', e.g. '"Hello " <> "world"'.
- Test the functions with:
let pair = new(1 "Hello ");
getFst(pair); getSnd(pair);
addConcat(pair new(99 "world!"));
Exercise: Integer/String pairs as abstract data type
Define a tuple value 'intString' aggregating a type 'T' (same as 'IntString' above) and functions 'new', 'getFst', 'getSnd', and 'addConcat' as components. Rewrite and use the tests of the previous exercise.
What is different? (note the bindings and scopes)
Note: This exercise demonstrates a) the orthogonality of Tycoon (types and functions in tuples), b) the Tycoon primitive for building abstract data types that encapsulates a type and functions on this type. Tycoon modules are only tuples in this sense and are implemented in this way.
Exercise: Orthogonality
The concept of orthogonality signifies that different constructs in
a language can be used with eachother in any way that seems to be
logical in terms of semantics. Here is a short exercise which will
make clear how this concept works.
Combine/nest the TL constructs tuple, array and function. Give nested types and the corresponding nested values,
e.g.
Let T =
Tuple
Let X =
Tuple
f1 :Tuple i :Int end
end
x :X
end;
let t =
tuple
Let X =
Tuple
f1 :Tuple i :Int end
end
let x =
tuple
let f1 = tuple let i = 1 end
end
end;
- Tuples with tuples, arrays, functions.
- Arrays of tuples, arrays of arrays, arrays of functions.
- Functions that get/return functions, tuples, arrays.
Ulrike Steffens, 1994; Update: Gerald Schröder, 08-may-1995