word

advertisement
60-100 CLASS TEST # 1 – September 29th 2012
12:00 am to 2:00pm
1. Write Miranda programs to do the following:
a) A constant-valued program called p1 which outputs a pair with two values in it.
The first value is your name, and the second value is the length of your name.
p1 = (“richard”,7)
p1 = (“richard”, #”richard”)
p1 = (richard, 7)
p1 = “richard”, 7
p1 = richard 7
Anything else – give to Dr Frost
10 marks
10 marks
8 marks
7 marks
5 marks
b) A program called p2 which uses p1 to output the last letter of your name. You
can assume that there is program called fst which returns the first value of a
pair given as input, and a program called snd which outputs the second value of
a pair given as input.
p2 = fst p1 ! (snd p1 – 1)
p2 = (fst p1) ! (snd p1 – 1)
p2 = fst p1 –- “richar”
p2 = fst p1 ! (#(fst p1) – 1)
p2 = fst p1 ! 6
Anything else – give to Dr Frost
10
10
10
10
10
marks
marks
marks
marks
marks
2. Write Miranda programs to do the following:
a) A program called p3 which takes a list of lists of numbers as input and outputs a
list containing the length of each list in the input list. You must use the built-in
map function. For example:
p3 [[1,5],[6,3,97,2],[11,5,7]] => [2,4,3]
p3 n = map (#) n
p3
= map (#)
p3 n = map # n
p3
= map #
p3 n = map length n
where length [] = 0
length (x:xs) = 1 + length xs
Anything else – give to Dr Frost
10
marks
10
marks
9 marks
9 marks
10
marks
b) A program called p4 which takes a list of positive numbers as input and which
outputs the largest number. You must use the built-in foldr function. You
may need to define your own operator. Examples of executing p4 are:
p4 [1,9, 2]
p4 [11,4,2,3]
=> 9
=> 11
p4 n = foldr my_op 0 n
where my_op x y = x, if x > y
= y, otherwise
10 marks
p4
= foldr my_op 0
where my_op x y = x, if x > y
= y, otherwise
10 marks
p4 n = foldr my_op (-1) n
where my_op x y = x, if x > y
= y, otherwise
10 marks
p4
10 marks
= foldr my_op (-1)
where my_op x y = x, if x > y
= y, otherwise
p4 n = foldr my_op n (missing 0)
where my_op x y = x, if x > y
= y, otherwise
8 marks
p4 n = foldr (>) 0 n (or -1 instead of 0)
8 marks
p4 n = foldr (>)
6 marks
p4
n
= foldr (>)
Anything else – give to Dr Frost
6 marks
3. Write conditional Miranda programs to do the following:
a) A program called p5 which takes two lists of numbers as input and which
outputs the string “yes” if the second value in the first list is larger than the second
value in the second list. You can assume that the lists are both of length greater than
5. For example:
p5 [1,2,3,4,5] [5,1,7,8]
p5 [1,2,3,4,5] [5,6,7,8]
p5 n m =
=
p5 n m =
p5 n m =
p5 n m =
=
p5 n m =
=
p5 n m =
=
p5 n m =
=
p5 n m =
=
Anything
=> “yes”(because 2 > 1)
=> “no” (because 2 is not > 6)
“yes”, if n!1 > m!1
“no”, otherwise
“yes”, if n!1 > m!1
“no”, otherwise
“yes”, if n!1 >= m!1
“no”, otherwise
“yes”, if n!1 >= m!1
“no”, if m!1 > n!1
“yes”, if n!1 > m!1
“no”, if n!1 <= m!1
“yes”, if n!2 > m!2
“no”, otherwise
yes, if n!1 > m!1
no, otherwise
else – give to Dr Frost
10 marks
10 marks
10 marks
10 marks
10 marks
9 marks
9 marks
b) A program called p6 which takes two non-empty lists as input and outputs the
length of the longest list. If the lists are of equal length, return the length of the first
list. An example of executing p6 is:
p6 [2,11,4,5] [3,9,6] => 4
p6 [4,5] [3,9,6]
=> 3
p6 n m =
=
p6 n m =
p6 n m =
p6 n m =
=
=
p6 n m =
p6 n m =
p6 n m =
p6 n m =
=
p6 n m =
=
#n, if #n > #m
#m, otherwise
#n, if #n > #m
#m, otherwise
#n, if #n > #m
#m, if #m > #n
#n, if #n = #m
#n, if #n > #m
#m, if #m > #n
#n, if #n = #m
#n, if #n >= #m
#m, otherwise
length n, if length n > lengthm
length m, otherwise
where length [] = 0
length (x:xs) = 1 + length xs
p6 n m = n, if #n > #m
= m, otherwise
Anything else – give to Dr Frost
10
marks
10
marks
10
marks
10
marks
10
marks
10
marks
9 marks
4. Write recursive Miranda programs to do the following:
a) A recursive program called p7 which takes a list of lists of numbers as input and
which returns a “flattened” list containing all of the numbers in the lists in the input.
For example:
p7 [[2,3], [4,2,6,7],[3]] => [2,3,4,2,6,7,3]
p7
[] = []
p7 (x:xs) = x ++ p7 xs
p7
[] = []
p7 (x:xs) = x ++ (p7 xs)
p7
[] = []
p7
n = hd n ++ p7 (tl n)
Missing base case
p7 (x:xs) = x ++ p7 xs
p7
[] = []
p7 (x:xs) = x : p7 xs
Anything else – give to Dr Frost
10 marks
10 marks
10 marks
7 marks
8 marks
b) A recursive program called p8 which takes a number and a list as input and which
returns True if the number is in the list, and False otherwise. For example:
p8 4 [2,4,3,5]
=> True
p8 4 [6,8,3,2,9] => False
HINT: you should recurse on the list, i.e. use pattern matching on the list, e,g.
p8 n
[] = ………
p8 n (x:xs)= ………
p8 n []
= False
p8 n (x:xs) = True, if x = n
= p8 n xs, otherwise
p8 n []
= False
p8 n (x:xs) = True, if x = n \/ p8 n xs
= False, otherwise
Missing base case
p8 n (x:xs) = True, if x = n
= p8 n xs, otherwise
p8 n []
= “False”
p8 n (x:xs) = “True”, if x = n
= p8 n xs, otherwise
Anything else – give to Dr Frost
10
marks
10
marks
8
marks
10
marks
5. Write the following Miranda programs:
a) A program p9 made up of parts which are connected together using the
.
compositional operator “ ” which takes a list of lists of numbers as input and
which outputs the value True if the length of the longest list is greater than 4
and False otherwise. For example:
p9 [[2,3],[5],[3,2,66,8,9]] => True
p9 [[4,3,2],[5,4],[3,1,22]] => False
You must use p3, p4, a program of your own and composition (.) in your
answer.
p9 n = (check . p4 . p3) n
where check n = True, if n > 4
= False, otherwise
p9 = check . p4 . p3
where check n = True, if n > 4
= False, otherwise
p9 n = (check . p3 . p4) n
where check n = True, if n > 4
= False, otherwise
p9 n = (p4 . p3) n
10 marks
10 marks
7 marks
7 marks
Anything else – give to Dr Frost
b) A program called p10 which takes two lists of numbers as input and which
outputs a list of pairs of numbers where the first number is from the first list, the
second number is from the second list, and the sum of the two numbers in each
pair is equal to 10. You must use a list comprehension in your answer. An
example of executing p10 is:
p10 [4,6,2,3,8] [4,2,6,7]=> [(4,6),(6,4),(3,7),(8,2)]
p10
p10
p10
p10
n
n
n
n
m
m
m
m
=
=
=
=
[(x,y)
[(x,y)
[(x,y)
[(x,y)
|
|
|
|
x
x
n
x
<->
->
<-
n;
n;
x;
n;
y
y
m
y
<->
->
<-
m;
m;
y;
m;
x
x
x
n
+
+
+
+
y
y
y
m
=
=
=
=
10]
10]
10]
10]
If a comma , is used instead of ; all alse correct
Anything else – give to Dr Frost
10 marks
8 marks
9 marks
8 marks
9 marks
Download