ASSIGNMENT :
1. Consider the following Prolog predicates which model looking up and installing variables and their values in a symbol table.
/*
lookup queries a symbol table for the value of a variable stored in the
symbol table.
lookup(Variable, Query_Value, [(Variable, Stored_Value) | _]) :-
!, Query_Value = Stored_Value.
lookup(Variable, Query_Value, [_ | Rest_of_Symbol_Table]) :-
lookup(Variable, Query_Value, Rest_of_Symbol_Table).
/*
install installs a variable and associated value in the symbol table.
install(Variable, Store_Value, [(Variable, Stored_Value) | _]) :-
Store_Value = Stored_Value.
install(Variable, Store_Value, [_ | Rest_of_Symbol_Table]) :-
install(Variable, Store_Value, Rest_of_Symbol_Table).
This program contains two constructions we haven’t seen before: 1) to represent a variable whose value we don’t care about, and 2) ! to represent a cut which blocks backtracking to retry any subgoals to its left. Note that lookup and install differ only in the use of the cut and the names of the variables Query Value and Store Value. Experiment with different queries to these two predicates to see how they behave, including queries which you would expect to fail. Based on your experiments, explain in detail how the cut works to distinguish the behavior of the two functions.
2. Write Prolog predicates to compare adjacent list elements and return a list, as described in each case. The sample responses are all with the input list [a, b, a, a, a, c, c]. Test your function on this list in each case.
(a) Remove the second and succeeding adjacent duplicates, yielding [a, b, a, c].
(b) Leave only the elements that are not consecutively repeated, yielding [a, b].
(c) Leave only one copy of repeated adjacent elements, yielding [a, c].
(d) Count the number of repeated adjacent occurrences, yielding [(1, a), (1, b), (3,
a), (2, c)].
1The final deadline for turning in this assignment is Friday, November 5, 2021, 11:59 P.M.
*/
*/


0 comments