Docsity

Prolog Exam II: Unification and Intersection Predicates, Exams of Programming Languages

Toggle sidebar
bg1
22C:111
page 1 of 2
Exam II
Open book/notes
1. [20 points]
Each part of this problem gives a pair of Prolog terms. You should determine
whether those two terms unify (= in Prolog); if so, provide the unifier/solution; if
not, describe the mismatch.
(a) p(f(A, g(B,C)), h(A,C)) = p(f(B, D), h(2,3))?
(b) p(A, B, 5) = p(2+3, C+1, A)?
2. [40 points]
This problem pertains to a predicate 'intersect(Xs,Ys,Zs)' that should succeed when list
Zs consists of the items in list Xs (in order) that also belong to list Ys. For instance,
intersect([1,2,3], [3,2,5], A) should succeed with A=[2,3], but we are also interested in
queries with variables in other positions.
Three potential definitions of 'intersect' are given below. For each pair of definitions,
determine if they yield the same results in all cases or not. If not, provide a query and the
differing responses, and otherwise explain why the results are always the same. For
completeness, the definitions of 'member' and 'not' also are included below.
(a) intersect1 vs.intersect2
(b) intersect1 vs. intersect3
(c) intersect2 vs. intersect3
intersect1([ ], Ys, [ ]).
intersect1([X|Xs], Ys, [X|Zs]) :- member(X,Ys), intersect1(Xs,Ys,Zs).
intersect1([X|Xs], Ys, Zs) :- not(member(X,Ys)), intersect1(Xs,Ys,Zs).
intersect2([ ], Ys, [ ]).
intersect2([X|Xs], Ys, [X|Zs]) :- member(X,Ys), intersect2(Xs,Ys,Zs).
intersect2([X|Xs], Ys, Zs) :- \+member(X,Ys), intersect2(Xs,Ys,Zs).
intersect3([ ], Ys, [ ]).
intersect3([X|Xs], Ys, Zs) :- intersect3(Xs,Ys,Ws),
(member(X,Ys) -> Zs=[X|Ws]; Zs=Ws).
member(X,[X|_]).
member(X,[_|Xs]) :- member(X,Xs).
not(Goal) :- ground(Goal), !, \+Goal.
not(Goal) :- write('unbound var in '), write(not(Goal)), abort.
predicate definitions
pf2

Partial preview of the text

Download Prolog Exam II: Unification and Intersection Predicates and more Exams Programming Languages in PDF only on Docsity!

22C:

page 1 of 2 Exam II Open book/notes

1. [20 points] Each part of this problem gives a pair of Prolog terms. You should determine whether those two terms unify (= in Prolog); if so, provide the unifier/solution; if not, describe the mismatch. (a) p(f(A, g(B,C)), h(A,C)) = p(f(B, D), h(2,3))? (b) p(A, B, 5) = p(2+3, C+1, A)? 2. [40 points] This problem pertains to a predicate 'intersect(Xs,Ys,Zs)' that should succeed when list Zs consists of the items in list Xs (in order) that also belong to list Ys. For instance, intersect([1,2,3], [3,2,5], A) should succeed with A=[2,3], but we are also interested in queries with variables in other positions. Three potential definitions of 'intersect' are given below. For each pair of definitions, determine if they yield the same results in all cases or not. If not, provide a query and the differing responses, and otherwise explain why the results are always the same. For completeness, the definitions of 'member' and 'not' also are included below. (a) intersect1 vs.intersect (b) intersect1 vs. intersect (c) intersect2 vs. intersect intersect1([ ], Ys, [ ]). intersect1([X|Xs], Ys, [X|Zs]) :- member(X,Ys), intersect1(Xs,Ys,Zs). intersect1([X|Xs], Ys, Zs) :- not(member(X,Ys)), intersect1(Xs,Ys,Zs). intersect2([ ], Ys, [ ]). intersect2([X|Xs], Ys, [X|Zs]) :- member(X,Ys), intersect2(Xs,Ys,Zs). intersect2([X|Xs], Ys, Zs) :- +member(X,Ys), intersect2(Xs,Ys,Zs). intersect3([ ], Ys, [ ]). intersect3([X|Xs], Ys, Zs) :- intersect3(Xs,Ys,Ws), (member(X,Ys) -> Zs=[X|Ws]; Zs=Ws). member(X,[X|]). member(X,[|Xs]) :- member(X,Xs). not(Goal) :- ground(Goal), !, +Goal. not(Goal) :- write('unbound var in '), write(not(Goal)), abort. predicate definitions

22C:

page 2 of 2

3. [40 points] For this problem, you are to write a Prolog definition for 'cumulative(List1,List2)' that succeeds when List1 is a list of numbers, and List2 is the list of cumulative sums of these numbers (i.e., item number n of List2 is the sum of the first n items of List1). For instance, cumulative([1,2,4], Sums) should succeed with Sums = [1,3,7]. Your solution will not receive full credit without an explanation of its operation, and justification that it behaves as required.