UGC-NET Jun 2012 · Uninformed Search (BFS, DFS, UCS)

MCQ · 2 marks · Problem AI-ASRC-0043

Which of the following prolog programs correctly implement “if G succeeds then execute goal P else execute goal $\theta$”?
  1. A
    if-else (G, P, $\theta$):-!,call(G), call(P). if-else (G,P, $\theta$) :- call($\theta$).
  2. B
    if-else (G, P, $\theta$):-call(G), !, call(P). if-else (G,P, $\theta$) :- call($\theta$).
  3. C
    if-else (G, P, $\theta$):-call(G), call(P), !. if-else (G,P, $\theta$) :- call($\theta$).
  4. D
    All of the above

Solve it on GATE DA practice →More Search Strategies problems

Show the worked solutionThe cut must come right after the test G succeeds. In (B), if call(G) succeeds, the cut commits to the first clause and P runs, so the second clause is never tried. If G fails, the cut is never reached and the second clause runs \(\theta\). This is exactly if-then-else.(A) The cut comes before G, so if G fails, the second clause is already cut off and \(\theta\) never runs — incorrect.(C) The cut comes after P, so if G succeeds but P fails, Prolog backtracks into the second clause and runs \(\theta\) as well — incorrect.(D) Incorrect, since (A) and (C) fail.Answer: B

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com