Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
60-100 CLASS TEST # 1 – October 3rd 2009 ANSWERS ARE ENCLOSED IN A BOX – NOTE THAT OTHER CORRECT ANSWERS ARE POSSIBLE AND WILL BE GIVEN FULL MARKS PARTIALLY CORRECT ANSWERS WILL BE GIVEN PART MARKS 1. PROGRAMMING 1.1. Write Miranda programs to do the following: a) A program called p1 which outputs the pair ([1,2], “abc”) p1 = ([1,2], “abc”) b) A program p2 which uses p1 to output the list [([1,2], “abc”), ([1,2], “abc”)] p2 = [p1, p1] OR p2 = p1:[p1] OR [p1] ++ [p1] 1.2. Write Miranda programs to do the following: a) A program called p3 which takes a list of numbers as input and outputs a list of lists, where each list at position p in the output contains the number from position p in the input. You must use the built-in map function . An example of executing p3 is: p3 [2, 7, 1, 11] => [[2], [7], [1], [11]] p3 = map makelist where makelist x = [x] OR p3 n = map makelist n where makelist x = [x] OR p3 = map (:[]) OR p3 n = map (:[]) n b) A program called p4 which takes a list of lists of numbers as input and which “flattens” the list into a single list of numbers. You must use the built-in foldr function . An example of executing p4 is: p4 [[2,1], [7, 8, 4], [6]]=> [2, 1, 7, 8, 4, 6] p4 = foldr (++) [] OR p4 n = foldr (++) [] n 1.3. Write Miranda programs to do the following: a) A program called p5 which takes two numbers as input and outputs the value True if their product is greater than 6 and False otherwise. An example of executing p5 is: p5 3 5 => True p5 2 2 => False (because 3 * 5 > 6) (because 2 * 2 is not > 6) p5 x y = p5 x y = OR p5 x y = = OR p5 x y = True, if x * y > 6 False, if x * y <= 6 True, if x * y > 6 False, otherwise (x * y) > 6 b) A program called p6 which takes two pairs of numbers as input and which outputs the pair with the largest value in the first position. If the pairs have the same value in the first position, then output the first pair. Examples of p6 are: p6 (4, 5) (2, 6) => (4, 5) p6 (6, 3) (8, 7) => (8, 7) p6 (4, 9) (4, 11) => (4, 9) p6 (a, b) (c, d) = (a, b), if a p6 (a, b) (c, d) = (c, d), if c p6 (a, b) (c, d) = (a, b), if a OR p6 p q = p, if fst p > fst q p6 p q = q, if fst q > fst p p6 p q = p, if fst p = fst q you can assume fst is available can define it as follows: fst (a, b) = a (because 4 > 2) (because 8 > 6) (because 4 = 4) > c > a = c in the library or you 1.4. Write Miranda programs to do the following: a) A recursive program called p7 which takes a list as input and which returns the list in reverse order. An example of executing p7 is: p7 [3, 2, 4] => [4, 2, 3] p7 [] = [] p7 (x:xs) = p7 xs ++ [x] b) A recursive program called p8 which takes a list as input and which returns the product of the numbers in the list. An example of executing p8 is: p8 [2, 3, 7] => 42 p8 [] = 1 p8 (x:xs) = x * p8 xs 1.5. Write Miranda programs to do the following: a) A program called p9 which takes a list of lists of numbers as input and which returns the product of the numbers in all of the sublists. You must use p4 and p8 and program composition ( . ) in your answer. An example of executing p9 is: p9 [[2, 4, 2],[3],[5, 2]] => 480 (because 2*4*2*3*5*2 = 480) p9 = p8 . p4 OR p9 n = (p8 . p4) n b) A program p10 which takes a list of numbers as input and which outputs a list containing the squares of all numbers in the input that are greater than 10. You must use a list comprehension in your answer. An example of p10 is: p10 [15,3,12,4,10] => [225,144] (because 15 and 12 are > 10) p10 n = [x * x | x <- n; x > 10] 2. DATA 2.1. Give an example of one instance of each of the following: a) a tuple containing three elements, the first being a number, and the second being a single character, and the third being a list of characters. (6, ‘d’, “fgh”) b) a list containing five elements. [11, 3, 56, 43, 2] Note – must all be of the same type 2.2. Define the following set operators in terms of the set membership operator Є. (Hint - You should give a formal definition, using the notation of logic (and a set comprehension for a)). ∩ a) Set intersection s ∩ t = {x | x Є s & b) The subset operator s OR s x Є t} t = True iff x x Є s -> x Є t t = True if x x Є s -> x Є t = False, otherwise 2.3. Use truth tables to show that: a) p True True False False (p -> q) is equivalent to (~p \/ q) q True False True False p -> q (~p \/ q) True True False False True True True True All the same, therefore equivalent b) p True True True True False False False False T = True 2.4. Let ((p -> q) & (q -> r)) -> (p -> r) is always True. q T T False False T T False False q -> r T False T T T False T T r T False T False T False T False p -> q T T False False T T T T & T False False False T False T T -> T T T T T T T T All true p -> r T False T False T T T T x = {3, 4}, y = {5, 8} What is the value of the following: cp (x, y, y) where cp is Cartesian Product {(3,5,5),(3,5,8),(3,8,5),(3,8,8), (4,5,5),(4,5,8),(4,8,5),(4,8,8)} OR in different order OR in a table 2.5. Consider the relation: r = {(x,y),(y,x),(y,z),(z,x)} a) What tuple(s) have to be added to make the relation symmetric? ADD (z,y) and (x,z) b) What tuple(s) have to be added to make the relation transitive? ADD (x,x),(y,y),(z,z),(x,z),(z,y) c) What tuple(s) have to be added to make the relation reflexive? ADD (x,x),(y,y),(z,z)