HOMEWORK 3. Deadlines: For the exercises 1-5: Monday, November 1. For the exercise 6-7: Friday, November 5. ====================== 1. Define the relation reverse_list(List, Reversed_List) that reverses lists, e.g.: ?- reverse_list([a,b,c,d],[d,c,b,a]). yes ?- reverse_list([a,b,c,d],X). X=[d,c,b,a]; No ===================== 2. 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]). yes ?- palindrome([s,i,r]). no ======================= 3. 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]; no ======================== 4. Define the relation flatten_list(InputList, FlatList) where InputList can be a list with nested occurrences of lists, and FlatList is InputList "flattened" so that the elements of InputList's sublists (or sub-lists, sub-sub-lists, etc) are reorganized as one plain list. For example, ?- flatten_list([a,b,[c,[d]],e],L). L=[a,b,c,d,e]; no ?- flatten_list([a,b,[c,[]],e],L). L=[a,b,c,e]; no ========================== 5. Write a predicate that accepts a list and succeeds if that list has exactly four elements, where the first and the third elements are the same. ========================== 6. Write a predicate that accepts a list and generates from it a similar list with the first two elements swapped. It should work like this: ?-swap_first_two([a,b,c,d], [b,a,c,d]). Yes ?-swap_first_two([a,b,c,d], X). X= [b,a,c,d]; No ?-swap_first_two(X, [b,a,c,d]). X=[a,b,c,d]; No ?-swap_first_two([a,b,c,d], [b,a,d,c]). No ?-swap_first_two([a,b,c,d], [X,Y,c,d]). X=b Y=a; No =============================== 7. Assume you have to fly from Linz to Stockholm, and want to examine possible connections between these two cities. There are no direct flights. Therefore, you have to transfer. Write a Prolog program that meets the requirements below: - Choose at least one city where you will transfer. - Represent the information on the flight timetable in a Prolog database. - Create the set of rules to find the possible flights given a started time and a maximum waiting time in the transfer city (giving the times of departure and arrival at each city). - Make use of structures, if possible. In addition, give a set of examples (just the question formulated) asking questions about the info in the timetable.