home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l210 / 1.ddi / EXAMPLES.ARC / CH07EX06.PRO < prev    next >
Encoding:
Prolog Source  |  1988-06-21  |  906 b   |  45 lines

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