Unformatted text preview:

CS 538Final Exam1. (a) (13 points) Write a Java method  static int[] filteredMap(int[] a, MapFct M, PredFct P...2. (25 points) Explain how type-inference works in ML. Illustrate the process by solving for the ...3. (a) (8 points) Write Prolog facts and rules that define the relation last(L,E). L is a non-emp...4. What do each of the following Python program fragments compute? In each case explain why. (a) ...6. (a) (13 points) Let f and g be ML functions and let L be a list containing the values [v1,v2,....CS 538Final ExamMonday, May 12, 20032:45 PM— 4:45 PM121 PsychologyInstructionsAnswer any four questions. (If you answer more, only the first four will count.) Eachquestion is worth 25 points. Please try to make your answers neat and coherent.Remember, if we can’t read it, it’s wrong. Partial credit will be given, so try to putsomething down for each question (a blank answer always gets 0 points!).1. (a) (13 points)Write a Java method static int[] filteredMap(int[] a, MapFct M, PredFct P)MapFct is an object that contains a single member function declared asstatic int f(int i) { ... }PredFct is an object that contains a single member function declared asstatic boolean p(int i) { ... }filteredMap takes an integer array as input. P.p first filters out array elementsthat evaluate to false. Then M.f is applied to remaining values, which are returned asthe result of the call.For example, if M.f is defined asstatic int f(int i) {return 2*i;}and P.p is defined asstatic boolean p(int i) {return i%2==0;}then with a ={1,2,3,4,5,6,7,8,9,10}, filteredMap will return the array{4,8,12,16,20}.(b) (6 points)Rewrite filteredMap in Pizza, utilizing its first-class functions as parameters twoand three.(c) (6 points)Extend your solution to part (b) to make filteredMap polymorphic. Now fil-teredMap should be able to handle any array of type T rather than just int arrays.-2-2. (25 points)Explain how type-inference works in ML. Illustrate the process by solving for thetype of the following functionfun r(f,[a,b]) = f(a,b) | r(f,u::x::y::z) = f(u,r(f,x::y::z));What does this function do?3. (a) (8 points)Write Prolog facts and rules that define the relation last(L,E). L is a non-empty listand E is its last (rightmost) element. Your solution should work for the case that L isknown (ground) and E is either unknown (free) or known (ground).(b) (17 points)Write Prolog facts and rules that define the relation palindrome(L). L is a list ofvalues (atoms or integers) that form a palindrome. A list is a palindrome if it has thesame contents when read from left to right or right to left. That is, the list’s first ele-ment is the same as its last element, its second element is the same as its second fromlast element, etc.For example, the following lists are all palindromes: [], [a], [1,1], [a,b,a],[1,2,2,1], [a,b,c,b,a].Your solution need only work for the case in which L is ground (known).4. What do each of the following Python program fragments compute? In each caseexplain why.(a) (9 points) a = [1,2,3,4,5]for i in a[:-2]:print a[:i]+(a[-i:])*i(b) (8 points)def f(a=1,b=2,c=3): return a+b+cprint f(f(),f(1,f(2)),f(2,3,f(4,5,6)))(c) (8 points)def add(v):return lambda li,val=v:[li]+[val]print map(add(10),range(1,6))-3-5. (25 points)Define a Prolog relation match(Input,Pattern) where Input is a list of singlecharacter lower-case input symbols (a to z) and Pattern is a list of pattern sym-bols. Pattern symbols are single lower-case characters, as well as the special patternsymbols, ? and +.Pattern symbols are matched against Input symbols, from left to right. In a pat-tern, a single letter symbol matches only itself. A ? symbol matches zero or oneinput characters, while a + in a pattern matches one or more consecutive charactersymbols in the Input list.Your definition should answer yes if the pattern symbols match the input symbols,and no if they do not.For example, the following queries should all answer yes:match([a],[a]). match([a,b],[a,b]).match([a,b],[+]). match([a,b],[?,?]).The following queries should all answer no:match([a],[b]). match([a,b],[a,a]).match([a,b],[b,+]). match([a,b],[+,+,+]).Your solution needs to only handle the case where Input and Pattern are bothground (known).Illustrate your answer by showing how the querymatch([a,b,c],[?,a,+]).is solved.6. (a) (13 points)Let f and g be ML functions and let L be a list containing the values [v1,v2,...,vn].Write an ML function pf(f,g,L) that computes a list with the following n values:[f(v1,vn),g(v2,vn-1),..., f(vn-1,v2),g(vn,v1)]What is the type of pf?(b) (12 points)Let f be an ML function and let L be a list containing the values [v1,v2,...,vn]. Writean ML function comp(f,L) that computes a list with the following n values:[f(v1),f(f(v2)),f(f(f(v3))),..., fn(vn)]What is the type of


View Full Document

UW-Madison COMPSCI 538 - CS 538 Final Exam

Download CS 538 Final Exam
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 538 Final Exam and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 538 Final Exam 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?