Information Technology Reference
In-Depth Information
2.
Consider the mystery predicates
p
and
q
defined by the following three clauses:
p(X) :- q(X,[],X).
q([],Y,Y).
q([E|L],Y,Z) :- q(L,[E|Y],Z).
Describe in English the lists
x
for which the predicate
p
(
)
x
holds.
3.
Explain informally why
[X,[1,X,3]]
does not unify with
[Y,Y]
. It turns out that
Prolog thinks these two lists
do
unify: it creates a
cyclic term
, which in this case is
a list that contains itself as an element.
4.
Use the
append
predicate to define a predicate
just_before
(
x
,
y
,
z
)
that holds
when
x
appears just before
y
as an element of list
z
. So, for example,
?- just_before(3,4,[1,2,3,4,5,6]).
Yes
?- just_before(3,5,[1,2,3,4,5,6]).
No
Hint
: This can be done with one clause. Look at how
elem2
is defined.
5.
The list version of the blocks world gave short definitions of the
above
and
left
predicates. Define the
on
predicate using the
just_before
predicate.
6.
Write Prolog clauses that define each of the following predicates:
a.
exactly_3
(
)
x
is true if
x
is a list with exactly three elements.
b.
at_least_3
(
)
is true if
x
is a list with at least three elements.
x
is true if
x
is list with at most three elements.
Hint:
Use four clauses.
d.
intersect
(
c.
at_most_3
(
x
)
is true if
x
and
y
are lists with an element in common.
intersect([1,2,3,4],[5,4,1,6])
holds, but
intersect([1,2,3,4],[5,6])
does not hold.
Hint:
Use
member
but no recursion.
e.
all_intersect
(
x
,
y
)
z
,
y
)
is true if every element of list
z
is a list
x
such that
intersect
(
holds.
all_intersect([[1,2,3],[5,4,6]],[3,4])
holds,
x
,
y
)
and
all_intersect([],[3,4])
holds, but
all_intersect([[1,2,3],[1,2,5],[5,4,6]],[3,4])
does not hold.
Hint:
Use
intersect
and recursion.