




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Leavens; Class: Programming Languages I; Subject: Computer Programming; University: University of Central Florida; Term: Fall 2009;
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Spring, 2009 Name:
COP 4020 — Programming Languages 1
This test has 7 questions and pages numbered 1 through 8. This test is open book and notes. If you need more space, use the back of a page. Note when you do that on the front. Before you begin, please take a moment to look over the entire test so that you can budget your time. Clarity is important; if your programs are sloppy and hard to read, you may lose some points. Correct syntax also makes a difference for programming questions. When you write Oz code on this test, you may use anything in the declarative model (as in chapters 2–3 of our textbook). So you must not use not use cells or the library functions IsDet and IsFree. But please use all linguistic abstractions and syntactic sugars that are helpful. You are encouraged to define functions or procedures not specifically asked for if they are useful to your programming; however, if they are not in the Oz base environment, then you must write them into your test. You can use the built-in functions in the Oz base environment like Append, Member, Max, Filter, Map, and FoldR.
Question: 1 2 3 4 5 6 7 Total Points: 10 10 15 15 10 20 20 100
Score:
MyLength: < fun {$ }:
that takes a list Lst of values of some type T and returns the number of elements in Lst, that is, it returns the size of Lst. Your solution must have iterative behavior, and must be written using tail recursion. Don’t use the built-in Oz function Length, don’t use any higher-order functions, and don’t use the Oz for loop syntax in your solution. (You are supposed to know what these directions mean.) The following are examples, that use the Test procedure from the homework. \insert ’MyLength.oz’ {Test {MyLength nil} ’==’ 0} {Test {MyLength [c]} ’==’ 1} {Test {MyLength [b a]} ’==’ 2} {Test {MyLength [b b a]} ’==’ 3} {Test {MyLength [a b b a]} ’==’ 4} {Test {MyLength [b d a c a b b a]} ’==’ 8} {Test {MyLength [um no no no no yes no yes no]} ’==’ 9} {Test {MyLength [4 0 7 5 5 5 1 2 1 2]} ’==’ 10}
SelectAndMap: < fun {$ < fun {$ S}: Bool> < fun {$ S}: T>}:
>
that for some types S and T takes three arguments: Ls, which is a list of values of type S, Pred, which is a boolean-valued function of type < fun {$ S}: Bool>, and F, which is a function of type < fun {$ S}: T>. The function you are to write, SelectAndMap, selects elements E of Ls for which {Pred E} returns true, and returns a list of the result of applying F to those selected elements (preserving the original order). The following are examples. \insert ’SelectAndMap.oz’ local fun {AlwaysTrue _} true end fun {Odd N} N mod 2 == 1 end fun {Even N} N mod 2 == 0 end fun {Double X} X2 end in {Test {SelectAndMap nil AlwaysTrue Double} ’==’ nil} {Test {SelectAndMap 4|5|6|7|nil AlwaysTrue Double} ’==’ 8|10|12|14|nil} {Test {SelectAndMap 4|5|6|7|nil Odd Double} ’==’ 10|14|nil} {Test {SelectAndMap 4|5|6|7|nil Even Double} ’==’ 8|12|nil} {Test {SelectAndMap 1|2|3|1|4|5|6|7|nil fun {$ X} X>3 end Double} ’==’8|10|12|14|nil} {Test {SelectAndMap 1|2|3|1|4|5|6|7|nil fun {$ X} X>3 end fun {$ X} X10 end } ’==’ 40|50|60|70|nil} {Test {SelectAndMap 1|2|3|1|4|5|6|7|nil fun {$ X} X =< 3 end fun {$ X} X end } ’==’ 1|2|3|1|nil} {Test {SelectAndMap 1|2|3|1|4|5|6|7|nil fun {$ _} false end fun {$ X} X end } ’==’ nil} {Test {SelectAndMap [o o p s l a] AlwaysTrue fun {$ X} [X] end } ’==’ [[o] [o] [p] [s] [l] [a]]} end
Your solution must use both Filter and Map (but you can also write additional helping functions if you wish).
Project: < fun {$ <List >
that for some types S and T, takes an association list, Pairs (that is, a list of #-tuples of S and T elements), and an element of type S, E, and returns a list of all the elements of type T to which E is associated in Pairs (by being in the same pair). That is, whenever there is a pair SVal#TVal in Pairs and E == SVal, then TVal is in the result. The relative order of the values of type T that are in the result should be the same as their relative order in Pairs. The following are examples. \insert ’Project.oz’ \insert ’TestingNoStop.oz’ {StartTesting ’Project’} {Test {Project [1#2 2#3 3#4 2#5 1#6] 2} ’==’ [3 5]} {Test {Project [a#1 a#0 b#2 b#~1 c#3 c#4 d#4 d#5] a} ’==’ [1 0]} {Test {Project [a#1 a#0 b#2 b#~1 c#3 c#4 d#4 d#5] b} ’==’ [2 ~1]} {Test {Project [a#1 a#0 b#2 b#~1 c#3 c#4 d#4 d#5] b} ’==’ [2 ~1]} {Test {Project [a#1 a#0 b#2 b#~1 c#3 c#4 d#4 d#5] c} ’==’ [3 4]} {Test {Project [a#1 a#0 b#2 b#~1 c#3 c#4 d#4 d#5] z} ’==’ nil} {Test {Project nil q} ’==’ nil} {Test {Project [q#ok] q} ’==’ [ok]}
〈Bexp〉 ::= andExp(〈Bexp〉 〈Bexp〉) | orExp(〈Bexp〉 〈Bexp〉) | notExp(〈Bexp〉) | comp(〈Comp〉) 〈Comp〉 ::= equals(〈Atom〉 〈Atom〉) | notequals(〈Atom〉 〈Atom〉)
Write a function Subst: < fun {$
Be sure to follow the grammar!
〈UnitTest S T〉 ::= testcase(arg: expect:
Write a function AllPass: < fun {$
Be sure to follow the grammar!