Download Prolog Programming A First Course

Transcript
Appendix D
Solutions and Comments on
Exercises for Chapter ??
D.1
Exercise 3.1
In this set of exercises, the solutions are slightly abbreviated. Even so, it
is likely that your solution is a subset of the solutions proposed here. The
most difficult issue is what happens on failure.
1.
Subgoals
a(1)
b(1,Y)
c(2)
a(1)
Comment
uses 1st clause
uses 1st clause
uses 1st clause
using 1st clause
Result
new subgoals
succeeds with Y=2
succeeds
succeeds
The goal is solved in a very straightforward way. There is no backtracking.
2.
Subgoals
a(2)
b(2,Y)
b(2,Y)
c(2)
a(2)
Comment
uses 1st clause
uses 1st clause
uses 2nd clause
uses 1st clause
using 1st clause
Result
new subgoals
fails
succeeds with Y=2
succeeds
succeeds
Here, we have a simple case where a failure forces the use of the second
half of the definition for b/2.
3.
155