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

  1. /*
  2.    Copyright (c) 1986, 90 by Prolog Development Center
  3. */
  4.    
  5. /* Three procedures that are like CH07EX04.PRO but not tail recursive; 
  6.    they run out of memory after a few hundred iterations. */
  7.  
  8. predicates
  9.    badcount1(real)
  10.    badcount2(real)
  11.    badcount3(real)
  12.    check(real)
  13.  
  14. clauses
  15. /* badcount1:
  16.    The recursive call is not the last step. */
  17.  
  18.    badcount1(X) :- 
  19.       write(X), nl,
  20.       NewX = X+1,
  21.       badcount1(NewX),
  22.       nl.
  23.  
  24. /* badcount2:
  25.    There is a clause that has not been tried
  26.    at the time the recursive call is made. */
  27.  
  28.    badcount2(X) :- 
  29.       write(X), nl,
  30.       NewX = X+1,
  31.       badcount2(NewX).
  32.  
  33.    badcount2(X) :- 
  34.       X < 0,
  35.       write("X is negative.").
  36.  
  37. /* badcount3:
  38.    There is an untried alternative in a
  39.    procedure called before the recursive call. */
  40.  
  41.    badcount3(X) :- 
  42.       write(X), nl,
  43.       NewX = X+1,
  44.       check(NewX),
  45.       badcount3(NewX).
  46.  
  47.    check(Z) :- Z >= 0.
  48.    check(Z) :- Z < 0.
  49.