Solutions to tutorial 2

Solutions to tutorial 2
I have interspersed comments with the questions. In some cases I just give an idea of
what is required in others I give the correct answer. In some cases you will just have to
think things out for yourself.
1(a) List the elements of these functions using the maplet notation x § y :
addthree = = {x: £x  6  x § x + 3}
This should be a set with the appropriate maplets
{ 0 § 0 + 3, 1 § 1 + 3, , 2 § 2 + 3, …, 5 § 5 + 3}
or better { 0 § 3, 1 § 4, , 2 § 5, …, 5 § 8}
double = = {t :£ 2  t  8  t § 2t}
again a set with the appropriate maplets
{ 2 § 4, 3 § 6,…,8 § 16}
toggle = = {n :£ 1  n  10  n § n mod 3}
"23 mod 3" is the remainder when 23 is divided by 3 (and so equals 2). Hence the
answer to the question is
{ 1 § 1 mod 3, 2 § 2 mod 3, 3 § 3 mod 3, 4 § 4 mod 3, …, 10 § 10 mod 3} or
better { 1 §1, 2 § 2, 3 § 0, 4 §1, 5 § 2, …, 8 § 2,… ,10 § 1}
mkset = = {x :X  x § {x}}, where X is the set {"mary", "jim", "peter"}
{ "mary" § {"mary"}, "jim" § {"jim"}, "peter" § {"peter"} }
plus = = {a, b, c :1..5  a + b = c  (a, b) § c}
I will leave this for you to do completely on your own – the answers above should
help.
(b)Give the range of each of the functions above.
The range is the set of things which are on the rhs of a maplet – so you can find the
range by looking at the answers above and listing the things to the right of the
maplet sign (that is " § ") Since it is easy I will only do one
ran addthreee = {3, 4, 5, 6, 7, 8}, which is the same as ran addthree =3..8.
(c)Use your functions to look up the answer to
(i) addthree(4) (ii) plus(3,2)
(iii) toggle 8
(iv) mkset ("jim")
again look up the answer - to find toggle 8 find what is on the rhs of the maplet
from 8 which is 2. So toggle 8 = 2!
d)List the sets
(i) {x :£ | toggle(x) = 2}
(ii) {x, y :£  plus(x, y)  1 × (x, y)}
Remember toggle is only defined for 0..10 – so {x :£ | toggle(x) = 2}={2,5,8}.
For the answer to plus list the pairs which satisfy plus(x,y) < 2 – but since x, y must
be at least 1 there are no pairs which work and so the answer is { } – the empty set.
2 The functions inc: ¤ ª¤ and dec:¤ ª¤ each take an integer as an argument and return
the next and previous integer respectively.
(a)Define the functions inc and dec
inc = = { x : ¤ × x § x + 1} . dec is similar but uses the - operator.
(b)Modify your definitions to make inc and dec only deal with natural numbers.
inc is easy since all you need to do is change the declaration to be a natural. dec
needs a bit more
dec = = { y :£ | 0 < y × y § y-1} We make sure to exclude 0 from the domain since if
we only have naturals we can’t have 0 – 1 since the answer is -1 which is not a
natural.
3 Let STUDENT be the set of all students at De Monfort University. Let ID be the set of
all possible student identification numbers. And let COURSE be the set of all the named
courses that the university offers. The function ids maps ID numbers to STUDENTs.
(a)Explain why it is appropriate that the function declaration ids :ID © STUDENT should
be a partial function.
Decide which of the following functions are total and which are partial. Give a reason for
your choice. Write down the function declaration.
(i)st_id maps students to their id numbers.
(ii)on_course maps students to the course they enrolled on.
(iii)comp_list maps computing courses to the sets of students on
each course
(c)Let se be a computing course. Use the function comp_list, described above, to write an
expression that gives the number of students on the se course.
Talk about this in class.
4 Use set notation to define the functions described by these Modula-2 functions:
(a)PROCEDURE Smaller (a, b :INTEGER) :INTEGER;
(*Returns the smaller of a and b*)
(b)PROCEDURE Diff (a, b :CARDINAL) :CARDINAL;
(*Returns the difference of a and b *)
For (a) we want answers that would give us maplets like (2,3) § 2 and (3,2) § 2 –
but whether we want the first thing from the pair or the second thing depends on
the relative size of the entries – so we use the idea of defining by cases. So we make
two definitions and take the union:
Smaller = ={x, y : ¤ | x < y × (x,y) § x} ¼ {x, y : ¤ | x ä y × (x,y) § y}
Part (b) is similar but the modula 2 type cardinal corresponds to naturals.
5 Given
f
==
{1 § 2, 2 § 4, 3 § 6, 4 § 9, 5 § 11}
g
==
{2 § 8, 4 § 27, 6 § 64}
S
==
{1, 3, 5}
T
==
{2, 4, 6, 8, 10, 12}
X
==
{1, 2, 3, 4, 5}
Y
==
{2, 4, 6, 9, 11}
determine each of the following:
(a)
SÂf
(f) (X \ S) Â f
(b)
TÂf
(g) ran (f ÅT)
(c)
f ÃT
(h) f à (Y \ T)
(d)
f à 5…10
(i) f É g
(e)
dom (S Ä f)
(j) g É f
This question is a straightforward application of the definitions – it should not
present any difficulties. I will give a couple of answers only
(a) {1 § 2, 3 § 6, 5 § 11} (c) {1 § 2, 2 § 4, 3 § 6 }
(i) {1 § 2, 2 § 8, 3 § 6, 4 § 27, 5 § 11, 6 § 64}
6 Below are samples from two database tables. The MPs table holds the names of all
current members of parliament and their party; the PMs table has the names of all prime
ministers, past and present, and the number of years (or part of a year) they were in
power. Assume that all the names in the NAME field are unique.
(a)The tables can be modelled by the functions MPs and PMs. Give the types of these
functions.
(b)Use set and function notation to specify the result of the following database queries:
(i)the number of years Margaret Thatcher was prime minister
(ii)Stephen Dorrel's party
(iii)a list of all the different parties in the current parliament. (Give
the result as a set.)
(iv)a sub-table of all the Labour and Liberal Democrat MPs
(v)a sub-table of prime ministers who have been in office for more
than 10 years
(c)What do the following expressions represent?
(i)( MPs ÃLab)  ( MPs ÅLab)
(ii)(dom PMs) Â MPs
(d)Suppose that in two years time John Prescott becomes prime minister in place of Tony
Blair. Write an expression to update the PMs table.
(e)Let New be a table (name and party) of all newly elected MPs , and let Gone be the set
of names of MPs not re-elected. Give an expression to update the MPs table.
This is a modelling exercise – discuss in the lab.
7 State the relationship between the size of the range and the size of the domain.
The range certainly can’t be bigger than the domain – but it might be smaller.
8 Let S == Ï h, e, l, l, o Ð, and T = = Ï w, o, r, l, d Ð
(a)Write S , T and S Ê T in maplet form
I will do a couple of these: S = {1 § h ,2 § e ,3 § l ,4 § l , 5 § o } and S Ê T = {1
§ h ,2 § e ,3 § l ,4 § l , 5 § o , 6 § w, 7 § o, 8 § r, 9 § l, 10 § d}
(b)What is
(i) S(2)
(ii) T(3)
(iii) (S Ê T)(6)
(iv) S(6) ?
(S Ê T)(6) = w
(c)Work out
(i)ran (S Ê T) = {h,e,l,o,w,r,d}
(ii)S drop 3 = <l, o> = {1 § l, 2 § o}
(iii)S take 4 = <h,e,l,l>
(iv)(S take 1) Ê aÊ (S drop 2)= <h,a,l,l,o>
(v){1, 3, 5} Â S ={1§ h, 3 §l, 5 § o}
(vi)(front (tail T)) Ê a,n,d Ê last S = <o, r, l, a, n, d, o>
9 The characters on a line of a text editor can be modelled as a sequence of numbers
where the numbers represent the ASCII codes. Let line :seq N be a line of characters.
The function insert(line, x, n) inserts the character with code x in the nth position of line.
The function delete(line, n) deletes the nth character from line.
The function substring(line, p, n) gives the sequence of n characters starting at position p
of line.
(a)Give an example of line, and the functions insert, delete, and substring to illustrate
what they do.
(b)Define insert, delete, and substring using the functions take and drop.
e.g. insert(line, x, n) = ( line take ? ) Ê x Ê ( line drop ? )
(c)Discuss whether your definitions work for all possible arguments or not.
Note: Characters 'A'..'Z' have ASCII codes 65..90, and charcaters 'a'..'z' have codes
97..122
Again this is a modelling exercise – discuss in tutorial.