home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l217 / 2.ddi / EXAMPLES / CH07EX06.PRO < prev    next >
Encoding:
Prolog Source  |  1990-03-26  |  846 b   |  42 lines

  1. /*
  2.    Copyright (c) 1986, 90 by Prolog Development Center
  3. */
  4.    
  5. /* Shows how badcount2 and badcount3 can be fixed by adding cuts to
  6.    rule out the untried clauses. These versions are tail recursive. */
  7.  
  8. predicates
  9.    cutcount2(real)
  10.    cutcount3(real)
  11.    check(real)
  12.  
  13. clauses
  14. /* cutcount2:
  15.    There is a clause that has not been tried
  16.    at the time the recursive call is made. */
  17.  
  18.    cutcount2(X) :- 
  19.       X>=0, !,
  20.       write(X),
  21.       nl,
  22.       NewX = X + 1,
  23.       cutcount2(NewX).
  24.  
  25.    cutcount2(_) :- 
  26.       write("X is negative.").
  27.  
  28. /* cutcount3:
  29.    There is an untried alternative in a
  30.    clause called before the recursive call. */
  31.  
  32.    cutcount3(X) :- 
  33.       write(X),
  34.       nl,
  35.       NewX = X+1,
  36.       check(NewX),
  37.       !,
  38.       cutcount3(NewX).
  39.  
  40.    check(Z) :- Z >= 0.
  41.    check(Z) :- Z < 0.
  42.