HOMEWORK DUE ON FRIDAY, NOVEMBER 30 ----------------------------------- 1. Write the following as a set of Prolog predicates: Eat at the restaurant if it looks nice but if it stinks don't eat there using: (a) cut and fail combination (b) the 'not' predicate. ============ 2. Consider the following program that defines the predicate 'insert'. The predicate is satisfied if its third argument is a sorted list obtained from its second argument, also a sorted list, by inserting in it the first argument: insert(X,[H|T],[H|T1]) :- X>H, !, insert(X,T,T1). insert(X,L,[X|L]). 1. Provide an appropriate query to show that this program is incorrect 2. Change the program so that it works correctly ============ 3. Write a ternary predicate delete_all(item,list,result) that is true if result is obtained from list by deleting all occurrences of item. Use cut to prevent backtracking. Sample run: ?-delete_all(a,[a,b,c,a,d,a],X). X=[b,c,d]; No ?-delete_all(a,[b,c,d],X). X=[b,c,d]; No ============= 4. Write a binary predicate count_occurrences(input,result) that is true if 'result' is a list of two-element lists [el, number_of_occurrences_in_input], where 'el' is an element of the list 'input', and 'number_of_occurrences_in_input' is an integer specifying how many times 'el' occurs in 'input'. For each element 'el' in 'input' there should be a corresponding pair [el, number_of_occurrences_in_input] in 'result'. Sample run: ?- count_occurrences([a,b,a,a,b,c],X). X = [[c, 1], [b, 2], [a, 3]] ; No ============= 5. Write a predicate count_and_print_occurrences that first asks the user to provide a list, then asks where to print the output (on the screen or in a file), counts occurrences in the list using the program developed in the exercise 4 above, and prints the output either on the screen or in a file specified by the user as follows: If, for instance, the count_occurrences program computes the answer [[c, 1], [b, 2], [a, 3]], then count_and_print_occurrences should print the following: c occurs in the input once. b occurs in the input twice. a occurs in the input 3 times. No other element occurs in the input. ============== 6. Write a ternary predicate delete_nth that deletes every N'th element from a list. Sample runs: ?- delete_nth([a,b,c,d,e,f],2,L). L = [a, c, e] ; No ?- delete_nth([a,b,c,d,e,f],1,L). L = [] ; No ?- delete_nth([a,b,c,d,e,f],0,L). No ?- delete_nth([a,b,c,d,e,f],10,L). L = [a, b, c, d, e, f] ; No ============== 7. Is the predicate p defined below tail recursive? Justify your answer. p([H|T]):-q,p(T). p([]). q.