Fall 2008 COP 4020 - Declarative Programming Test Solutions - Prof. Gary Leavens, Exams of Programming Languages

The solutions to the declarative programming techniques test for the cop 4020 programming languages 1 course at the university level. The test includes five questions that require writing iterative functions, functions, and higher-order functions using oz language and tail recursion. The document also includes examples and tests for each question.

Typology: Exams

Pre 2010

Uploaded on 11/08/2009

koofers-user-4hp
koofers-user-4hp ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Fall, 2008 Name:
COP 4020 โ€” Programming Languages 1
Test on Declarative Programming Techniques
Special Directions for this Test
This test has 5 questions and pages numbered 1 through 7.
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 imperative features (such as cells and assignment) or the library functions IsDet and
IsFree.
You are encouraged to define functions or procedures not specifically asked for if they are useful to your program-
ming; 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,Filter,Map, and FoldR.
For Grading
Problem Points Score
1 15
2 15
3 20
4 15
5 35
pf3
pf4
pf5

Partial preview of the text

Download Fall 2008 COP 4020 - Declarative Programming Test Solutions - Prof. Gary Leavens and more Exams Programming Languages in PDF only on Docsity!

Fall, 2008 Name:

COP 4020 โ€” Programming Languages 1

Test on Declarative Programming Techniques

Special Directions for this Test

This test has 5 questions and pages numbered 1 through 7.

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 imperative features (such as cells and assignment) or the library functions IsDet and

IsFree.

You are encouraged to define functions or procedures not specifically asked for if they are useful to your program-

ming; 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, Filter, Map, and FoldR.

For Grading

Problem Points Score

1. (15 points) [UseModels] Write an iterative function

Find: < fun {$ T}: Int>

that takes a list Elems of values of some type T and a value Sought of type T , and returns the first index

(counting from 1) in Elems whose value is equal to Sought (using ==), if it exists. If there is no element in

Elems that is == to Sought, then the result is ~1.

Your solution must have iterative behavior, and must be written using tail recursion. Donโ€™t use any higher-order

functions or 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.

{Test {Find [a b c d] a} โ€™==โ€™ 1} {Test {Find [a b c d] e} โ€™==โ€™ ~1} {Test {Find [a b c d e f g] c} โ€™==โ€™ 3} {Test {Find [[99] [86] [12 22] nil [3]] [3]} โ€™==โ€™ 5} {Test {Find [nil [3]] [3]} โ€™==โ€™ 2} {Test {Find nil 72} โ€™==โ€™ ~1} {Test {Find [penn ohio flor iowa] vote} โ€™==โ€™ ~1}

3. (20 points) [UseModels] Write a higher-order function

StateTemps: < fun {$ Int Int} : < fun {$ }: >>

that takes two integers, Low and High, and returns a function that takes a list of integers Temps and produces

a list that contains the elements of Temps that are between Low and High (inclusive), in their original order.

The following are examples.

local FLTemps = {StateTemps 60 90} in {Test {FLTemps nil} โ€™==โ€™ nil} {Test {FLTemps [60 90 32 72]} โ€™==โ€™ [60 90 72]} {Test {FLTemps [59 91]} โ€™==โ€™ nil} {Test {FLTemps [100 95 90 85 80 75 70 65 60 55 50 45 40]} โ€™==โ€™ [90 85 80 75 70 65 60]} {Test {FLTemps [~2 10 5 91 77 3 77 5 89 89 77]} โ€™==โ€™ [77 77 89 89 77]} end local IATemps = {StateTemps ~30 100} in {Test {IATemps [60 90 0 ~4 ~30 ~33 32 172]} โ€™==โ€™ [60 90 0 ~4 ~30 32]} {Test {IATemps [~20 10 5 101 100 3 100 5 89 89 100]} โ€™==โ€™ [~20 10 5 100 3 100 5 89 89 100]} end {Test {{StateTemps ~50 65} [~51 ~50 60 ~50]} โ€™==โ€™ [~50 60 ~50]}

4. (15 points) [UseModels] Using FoldR, write the function

ApplyList: < fun {$ <List < fun {$ T}: S>> T}: >

that for some types T and S, takes a list of functions Funs (which has type <List < fun {$ T}: S>>),

and a value X of type T, and returns a list that results from applying each element of Funs to X, and returning a

list of the results (preserving the order of the Funs list). That is, for functions F 1 , F 2 ,.. ., Fn, and value X, we

have:

{ApplyList [F 1 F 2... Fn] X} == [{F 1 X} {F 2 X}... {Fn X}]

The following are examples.

{Test {ApplyList [Not Not] true } โ€™==โ€™ [ false false ]} {Test {ApplyList [ fun {$ X} bread#X#bread end fun {$ X} pita#X#pita end ] turkey} โ€™==โ€™ [bread#turkey#bread pita#turkey#pita]} {Test {ApplyList nil 33} โ€™==โ€™ nil} {Test {ApplyList [ fun {$ X} X+1 end ] 4020} โ€™==โ€™ [4021]} {Test {ApplyList [ fun {$ X} X+1 end fun {$ X} X+2 end ] 4020} โ€™==โ€™ [4021 4022]} {Test {ApplyList local AddC = fun {$ Y} fun {$ X} X+Y end end in {Map [1 2 3 4 5 2 27 999] AddC} end 1000} โ€™==โ€™ [1001 1002 1003 1004 1005 1002 1027 1999]}

Since you must use FoldR, write your answer by filling in the following outline (you can also write helping

functions if you wish).

fun {ApplyList Funs X}

{FoldR

end

Put your answer to the OptimizeIfs problem below.

%% Assume this function in your solution %% NecessarilyTrue : <fun {$ }: Bool> fun {NecessarilyTrue Exp} case Exp of equalsExp(varExp(V1) varExp(V2)) then V1 == V [] equalsExp(numExp(N1) numExp(N2)) then N1 == N else false end end