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

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