SOLUTIONS RATED A+
✔✔What is wrong with this piece of Prolog code?
ancestor(A, D) :- ancestor (A, P), ancestor(P, D). - ✔✔It does not have a stopping
condition.
✔✔Explain how the following rules check if there is a miscolor in a map factbase.
adjacent(X, Y) :- edge(X, Y); edge(Y, X).miscolor(S1, S2, Color1) :- adjacent(S1, S2),
color(S1, Color1),color(S2, Color1). - ✔✔Prolog runtime will iteratively apply the rules to
all the facts to find the matches and mismatches.
✔✔Assume that the rule factorial(N, Fac) will compute Fac = N!. What should be the
output if the following question is asked?
?- factorial(2, 5). - ✔✔no
✔✔If we try to use tail-recursive rules to implement non-tail-recursive rules, it will
normally result in rules that are - ✔✔more complex and more difficult to understand.
✔✔Assume that the rule factorial(N, Fac) will compute Fac = N!. What should be the
output if the following question is asked?
?- factorial(N, 2). - ✔✔instantiation_error
✔✔Given a Prolog pair [H | T], which of the following statements are correct? Select all
correct answers.
- [H | T] is always a list, regardless whether H or T is a list.
- If H is not a list, then [H | T] is not list.
- If T is not a list, then [H | T] is not list.
- If T is a list, then [H | T] is list. - ✔✔- If T is not a list, then [H | T] is not list.
- If T is a list, then [H | T] is list.
✔✔What goal will return a "no" answer?
- member(pig, [cat | [dog | pig]]).
- member(dog, [cat | [dog | pig]]).
- None of them
- All of them
- member(cat, [cat | [dog | pig]]). - ✔✔member(pig, [cat | [dog | pig]]).
✔✔Which of the followings is equivalent to this Prolog list: [cat | [dog, pig]] ? - ✔✔[cat,
dog, pig]
✔✔the following rules will trigger a singleton variable warning. How di you fix the
problem?
, count([], 0) :- !.
count([X | Tail], S) :- count(Tail, S2), S is 1+S2. - ✔✔change X to _
✔✔which sorting algorithm has the best average execution time? - ✔✔quick sort
✔✔which sorting algorithm has the best execution time in the worst case scenario? -
✔✔merge sort
✔✔given the following set of the recursive rules, what clause represents the size-(N-1)
problem?
bar([],[]).
bar([X | L], F) :- bar(L, G), append(G, [X], F). - ✔✔bar(L, G)
✔✔what does the following set of the recursive rule do?
bar([],[]).
bar([X | L], F) :- bar(L, G), append(G, [X], F). - ✔✔reverse a list
✔✔which of the following statement correctly defines the not(X) rule? - ✔✔not(X) :- X, !,
fail.
not(_).
✔✔what is true about the cut (!)? - ✔✔- cut (!) can be placed anywhere in a rule as a
condition, and the effects can be different
- cut(!) removes all existing backtracking points, but new backtracking points can be
added later
✔✔True or false? A cut interferes with a recursive process by removing the recursive
exit points - ✔✔false
✔✔the flow control clause repeat in Prolog performs the following operation: -
✔✔generate a new backtracking point
✔✔we can use a repeat clause to form a loop. what clause we typically use to return to
the repeat point? - ✔✔fail
✔✔what is static memory - ✔✔allocated memory during compilation before the program
executes; global and static variables and objects
✔✔What is stack memory? - ✔✔allocated for all non-static local variables and objects
and member functions i.e. variables within functions; deallocated when out of scope