November 6, 1995
Due in quiz sections November 21, 1995
?- append([a,b,c],[w,x,y,z],L). ?- append([a,b],Y,[a,b,c,d]). ?- append([a,c],Y,[a,b,c,d]). ?- append(X,Y,[a,b,c,d]). ?- append(X,Y,Z).You don't have to hand in anything for this question.
/* What you like should go here */
next_color relation. (This is just a Prolog version of the
Lisp function you wrote for Question 5 in Assignment 1.) The
next_color relation has two arguments -- it should succeed if
both arguments are atoms representing stoplight colors, and if the second
argument is the color that follows the first argument. Otherwise it should
fail. Examples:
next_color(green,yellow).
next_color(yellow,red).
next_color(red,green).
twice rule that takes two arguments. (This
is also a Prolog version of a previous Lisp question.)
A call to twice succeeds
if both arguments are lists of the same length. Each element
in the second list should itself be a list of length two, with
the original element repeated. Examples:
twice([A],S).) Again, try backtracking in each case and see
if there are more answers. Then try the following goal and see what is
produced upon backtracking:
twice([],[]).
twice([X|Xs],[[X,X]|Ys]) :-
twice(Xs,Ys).
twice,
called flat_twice, that succeeds if the second list is twice
as long as the first, with each element of the first list repeated.
Examples:
flat_twice([],[]).
flat_twice([X|Xs],[X,X|Ys]) :-
flat_twice(Xs,Ys).
~borning/341/deriv.pl contains the ever-popular
symbolic differentiation program, this time in Prolog. The goal
go1(X) will try the first example, and similarly for
go2(X) and so forth.
The simplification rules contain a number of cuts (the "!" symbol). Why are these there? Try backtracking on the goals with the program as given. Now remove the cuts and try backtracking again. Explain the resulting behavior.
With the cuts, the program produces the correct answers, and if one tries to backtrack on any of the sample goals, it fails. Without the cuts, the first answers returned will be the same as for the original program. However, one can backtrack and get alternate answers. These alternate answers will still be correct but won't be in the simplest form. (For example, for go1(N), one gets N=1+0 in addition to the correct answer of N=1.)
The reason for this is that in the rules for plus_simplify and times_simplify, there are a series of simplifications -- for example, for adding 0 to anything -- and after these there is a catch-all rule that doesn't simplify at all. With the cuts, the catchall rule is encountered only if none of the previous simplications can be performed -- otherwise the cuts remove this rule as a possibility. Without the cuts, the catchall rule will be encountered on backtracking, and answers not in the simplest form may be returned.