Turbo Prolog
Turbo Prolog
Turbo Prolog
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Definition 3<br />
singlepeak([l,_).<br />
singlepeak([_l,_).<br />
singlepeak([U,VIW1,up):-<br />
UV,singlepeak([VIW1,down).<br />
Rule 4. Let <strong>Turbo</strong> <strong>Prolog</strong>'s unification mechanism do as much of the work as possible. At<br />
first thought, you might think to define a predicate equal to test two lists for equality as<br />
equal ( [ 1 , [ 1 ) .<br />
equal([UIX1,[UIY1):- equal(X,y).<br />
but this is unnecessary. Using the definition<br />
equal(X,X).<br />
<strong>Turbo</strong> <strong>Prolog</strong>'s unification mechanism does all the work!<br />
Rule 5. Use backtracking instead of recursion to effect repetition. Backtracking<br />
decreases stack requirements. The idea is to use the repeat . .. fail combination instead<br />
of recursion. This is so important that the next section is dedicated to the technique.<br />
Use of the Fail Predicate<br />
To have a particular sequence of subgoals evaluated repeatedly, it is often necessary to<br />
define a predicate like run with a clause of the form<br />
run:- readln(X),<br />
process(X,y),<br />
write(Y) ,<br />
run.<br />
thus incurring unnecessary tail recursion overheads that cannot be automatically eliminated<br />
by the system because process(X, Y) involves backtracking.<br />
In this case, the repeat . .. fail combination avoids the need for the final recursive call.<br />
Given<br />
repeat.<br />
repeat:-repeat.<br />
we can redefine run without tail recursion as follows:<br />
run:- repeat,<br />
readln(X),<br />
process(X,y),<br />
write(Y) ,<br />
fail.<br />
fail causes <strong>Turbo</strong> <strong>Prolog</strong> to backtrack to process, and eventually to repeat, which always<br />
succeeds.<br />
148 <strong>Turbo</strong> <strong>Prolog</strong> Owner's Handbook