It's All The Same To Me
You are to create the following two relations in Prolog:
monotonous(X)
The relation
monotonous(X) holds when
X is a list where no entry differs from any other.
?- monotonous([a,a,a]).
Yes
?- monotonous([w]).
Yes
?- monotonous([a,b,c]).
No
?- monotonous([penguin,penguin,penguin,seal,penguin,penguin]).
No
?- monotonous([we|X]).
X = [] ;
X = [we] ;
X = [we, we] ;
X = [we, we, we] ;
X = [we, we, we, we]
Yes
?-
The empty list is monotonous, but,
of course,
monotonous([c,s,c,4,0,4]) does not hold.
sublist(A,B)
The
sublist relation is satisfied when
A and
B are lists,
and
B contains each item in
A in the same order.
?- sublist([],[a,b,c]).
Yes
?- sublist([w,z],[u,v,w,x,y,z]).
Yes
?- sublist([m,x],[u,v,w,x,y,z]).
No
?- sublist([z,y,x],[w,x,y,z]).
No
?- sublist(X,[a,b,c]).
X = [] ;
X = [c] ;
X = [b] ;
X = [b, c] ;
X = [a] ;
X = [a, c] ;
X = [a, b] ;
X = [a, b, c] ;
No
?-
General Hints
When you wish to use a variable exactly once in a rule, you may
use an underscore, as in this length relation:
len(0,[]).
len(N, [_ | Tail]) :- len(N2, Tail), N is N2 + 1.
If you were to use a regular variable, as:
len(0,[]).
len(N, [X | Tail]) :- len(N2, Tail), N is N2 + 1.
Prolog would say something like:
Warning: (/your/file/name.pl:2):
Singleton variables: [X]
This is a only warning, and your code will operate correctly.
Be careful, however, because singleton variables often
are errors
(hence the warning).
Also, note that if you use
_
multiple times in a rule, each
_
is a separate variable.
Specific Hints
My
monotonous definition
has three rules. The first two
are facts (basis cases), and the last one is recursive having the
form
monotonous([X,X|Z]) :- . . .
This rule matches lists whose first two members are the same.
Make sure its condition requires all the members of
Z to be
the same as
X by recurring on a shorter list.
My sublist also has three cases, one
as a fact (basis case), and two recursive rules.
The recursive rules work the lists down
to empty. One handles the case of
a first item
in both lists, and one handles the
case of a first item in only the second list.
Submission
Define both relations in one file.
When they work properly, and are nicely formatted and documented,
submit the file using
this form.
Please make sure to submit something, even if only some of the relations
are correct.