ASSIGNMENT 1. --------------------------------------------------------- ========================================================= 1. Exercise 1.2 from the book: Program(Queen Victoria's family): male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):- female(X), parents(X,M,F), parents(Y,M,F). On the query sister_of(X,Y) more one answer can be returned. Explain how all the answers are obtained, and what they are. ========================================================= 2. Exercise 2.1 from the book: Say whether the following goals would succeed, and which variables, if any, would be instantiated to what values: pilots(A,london) = pilots(london,paris). point(X,Y,Z) = point(X1,Y1,Z1). letter(C) = word(letter). noun(alpha) = alpha. 'vicar' = vicar. f(X,X) = f(a,b). f(X,a(b,c)) = f(Z,a(Z,c)). =========================================================== 3. Write a predicate that accepts a list and succeeds if its second element is exactly the same as its fourth element. You can assume that the input list does not contain variables. =========================================================== 4. Write a binary predicate swap_and_insert that succeeds if its second argument is a list obtained from its first argument (also a list) by swapping the first two elements and inserting 'a' as the second element and 'b' as the fourth element. It should work like this: ?- swap_and_insert([1,2,3,4],[2,a,1,b,3,4]). true. ?- swap_and_insert([1,2,3,4],X). X = [2, a, 1, b, 3, 4]. ?- swap_and_insert(X,[2,a,1,b,3,4]). X = [1, 2, 3, 4]. What does it return on swap_and_insert(X,Y)? ============================================================ 5. Define the predicate palindrome(List) that recognizes palindromes. A list is a palindrome if it reads the same in the forward and in the backward direction. For example: ?- palindrome([m,a,d,a,m]). true ?- palindrome([s,i,r]). false ============================================================= 6. Define the relation shift(List1, List2) so that List2 is List1 "shifted rotationally" by one element to the left. For example, ?- shift([1,2,3,4,5], L1), shift(L1,L2). L1=[2,3,4,5,1] L2=[3,4,5,1,2]; false ============================================================= 7. Define the relation maximum(List,Integer) that succeeds if Integer is the maximal element of the list of integers List. Example, ?- maximum([1,2,3,4,2,-1,4],Max). Max=4; false ?- maximum([],Max). false =============================================================== 8. Write a binary predicate whose first argument is a natural number and the second argument is the sum of naturals from 1 to that natural number. Write two versions of this predicate, with and without an accumulator and compare their behavior on big inputs (1000000 and above, for instance). What differences do you notice? ============================================================== 9. Write a predicate twice_as_long(L1,L2) that succeeds if the list L2 is twice as long as the list L1. Do NOT compute the lengths of the lists. Sample run: ?- twice_as_long([],[]). true. ?- twice_as_long([a],[1,2]). true. ?- twice_as_long([a,b],X). X = [_G328, _G331, _G334, _G337] ; false ============================================================= 10. Write a predicate fib(N,F) that is true if F is the Nth Fibonacci number. Compute fib(5,F), fib(10,F), fib(50,F).