Docsity

Final Exam Problems - Programming Language Concepts | 22C 111, Exams of Programming Languages

Toggle sidebar
bg1
22C:111
page 1 of 3
Final Exam
Open book/notes
1. [25 points]
Consider the BNF definition X ::= e | XaXbX. For each of the strings below, show a
derivation (or parse) tree if the string is in the language L(X), and if not, briefly explain
why.
(a) ababab
(b) abbaab
(c) aababb
(d) aabbab
2. [25 points]
Consider the Haskell functions f and g defined below.
(a) what is the type of f?
(b) what is the type of g?
(c) what is the value of f 95?
(d) what is the value of g [1,2]
f n
| n>100 = n–10
| n<=100 = f (f (n+11))
g [ ] = [ ]
g [x] = [[ ], [x]]
g (x:xs) = g xs ++ [x:ys | ys<–g xs]
function definitions
3. [40 points]
Write a definition of an operation 'center', taking three arguments, a list, a non-negative
integer, and a “fill” item, and whose result is a list of length given by the integer
argument with the argument list “centered” in it (i.e., the number of fill items preceding
and following should differ by at most one), and all other positions of the result
containing the fill item For instance (using Haskell notation) center "abc" 6 '#' should
yield "#abc##" (or "##abc#").
pf3

Partial preview of the text

Download Final Exam Problems - Programming Language Concepts | 22C 111 and more Exams Programming Languages in PDF only on Docsity!

Final Exam

Open book/notes

1. [25 points]

Consider the BNF definition X :: = e | XaXbX. For each of the strings below, show a

derivation (or parse) tree if the string is in the language L(X), and if not, briefly explain

why.

(a) ababab

(b) abbaab

(c) aababb

(d) aabbab

2. [25 points]

Consider the Haskell functions f and g defined below.

(a) what is the type of f?

(b) what is the type of g?

(c) what is the value of f 95?

(d) what is the value of g [1,2]

f n

| n>100 = n–

| n<=100 = f (f (n+11))

g [ ] = [ ]

g [x] = [[ ] , [x]]

g (x:xs) = g xs ++ [x : ys | ys<–g xs]

function definitions

3. [40 points]

Write a definition of an operation 'center', taking three arguments, a list, a non-negative

integer, and a “fill” item, and whose result is a list of length given by the integer

argument with the argument list “centered” in it (i.e., the number of fill items preceding

and following should differ by at most one), and all other positions of the result

containing the fill item For instance (using Haskell notation) center "abc" 6 '#' should

yield "#abc##" (or "##abc#").

Provide a definition of the function 'center'. The definition can be written in Haskell or

Prolog. Full credit will be awarded only if you state the language you use and provide

justification that your program operates as required.

4. [40 points]

For the Prolog program given below, show all of the alternative responses to the query ?-

p(X,[a,a,b,b]).

p(p1, [ ]).

p(p2(T1,T2), [a|Xs]) :-

append(Ys,Zs,Xs), p(T1,Ys), q(T2,Zs).

q(q1, [ ]).

q(q2(T), [b|Xs]) :- q(T,Xs).

append([ ], Ys, Ys).

append([X|Xs], Ys, [X|Zs]) :- append(Xs,Ys,Zs).

predicate definitions

5. [40 points]

(a) [30 points]

Provide a Prolog definition of the predicate 'repeats(Xs,Ys)' that succeeds when Ys is

the list of distinct items that appear more than once in list Xs (i.e., one occurrence in Ys

of each repeated item in Xs). Your predicate should be able to solve for Ys given Xs.

For instance, the query 'repeats([a,b,c,c,a], As)' should succeed with As=[a,c].

(b) [10 points]

Show all the alternative responses of your answer to part (a) for the query

'repeats([a,A,b], Bs)'.

Full credit will be awarded only if you provide justification that your solution operates as

required.