home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / borland / jnfb88.arc / TAIL.ARC / CUTCOUNT.PRO < prev    next >
Text File  |  1987-10-22  |  912b  |  43 lines

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