Homework Assignment 7

advertisement
Homework Assignment 7
1. (40 points) Write Prolog rules and facts for swap (X, Y) such that Y is the list resulting
from swapping the first and the last item in X.
2. (30 points) Write Prolog rules and facts for modify(I, V, X, Y) such that Y is the list
resulting from modifying one item in list X (Y[I] = V, and all Y[J] = X[J], I!=J).
3. (30 points) Let two vectors be (x1, x2, …, xn) and (y1, y2, …yn), the distance of the
two vectors is defined as sqrt((x1-y1)*(x1-y1) + (x2-y2)*(x2-y2) + … + (xn-yn)*(xnyn)). Write Prolog rules and facts distance(V1, V2, D) where D is the distance of V1
and V2.
Submit your working program (hw7.pl) that includes the implementation of the above
functions. Programs that do not compile or do not produce any sensible results get 10
points at most. The file should include any additional helper axioms if necessary. A
reminder: This is an individual assignment.
An example execution of the program follows.
?- ['hw7_2014.pl'].
% hw7_2014.pl compiled 0.00 sec, -96 bytes
true.
?- swap([1,2,3,4,5,6,7,8,9], L).
L = [9, 2, 3, 4, 5, 6, 7, 8, 1] .
?- swap([10,20,30], L).
L = [30, 20, 10] .
?- modify(2, 100, [2,5,6,10,11,20], L).
L = [2, 5, 100, 10, 11, 20] .
?- modify(10, 10, [2,5,6], L).
L = [2, 5, 6] .
?- distance([1,2], [1,2], D).
D = 0.0.
?- distance([1,2,3], [0,0,0], D).
D = 3.74166.
?-
Download