home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / MISCTI10.ZIP / TI402.ASC < prev    next >
Encoding:
Text File  |  1988-05-02  |  2.6 KB  |  75 lines

  1. Although Turbo Prolog is a descriptive computer language, it is
  2. often desirable to repeat a process more than once.  This is
  3. known as creating a loop.  Implementing a loop in Turbo Prolog is
  4. challenging to most novice users.
  5.  
  6. There are two main ways to create a loop in Turbo Prolog,
  7. recursively and iteratively.  The first way, recursion, can be
  8. the most straightforward, but due to the demands made on RAM, the
  9. iterative approach is also a desirable technique.
  10.  
  11. In showing how loops are implemented, we will construct two short
  12. programs which count up to 100.  The first program, the recursive
  13. approach, is similar to a FOR loop implemented in other computer
  14. languages.
  15.  
  16. /*  RECURSIVE COUNTER */
  17. PREDICATES
  18.    count(integer)
  19. CLAUSES
  20.    count(100) :- !.
  21.    count(X) :- Y = X + 1 ,
  22.                write(Y), nl ,  /* body of loop goes here */
  23.                count(Y).
  24. GOAL
  25.    count(0).
  26. /*   END   */
  27.  
  28. Of course, there are many variations to this small program.  If
  29. recursion is still a mystery to you, follow this program through
  30. in the trace mode.
  31.  
  32. The next program shows a more complicated loop, yet the result is
  33. the same as the previous program.  This program works on a
  34. repeat/fail technique, and therefore can be used when available
  35. memory becomes a consideration.
  36. /*  ITERATIVE LOOP  */
  37. DATABASE
  38.    counter(integer)
  39. PREDICATES
  40.    my_retract(dbasedom)
  41.    count
  42.    repeat
  43. CLAUSES
  44.    count :- asserta( count(0) ) , fail.  /* initialize counter */
  45.    count :- repeat ,
  46.             count(X) ,
  47.             Y = X + 1 ,
  48.             my_retract( count(X) ) ,
  49.             asserta( count(Y) ) ,
  50.             write(Y), nl ,
  51.             Y = 100.
  52.  
  53.    my_retract(X) :- retract(X), !.  /* deterministic retract */
  54.  
  55.    repeat.   repeat :- repeat.
  56. GOAL
  57.    count.
  58. /*   END   */
  59.  
  60. Here, the body of the loop is placed between the repeat and the
  61. failing condition.  Notice that the failing condition succeeds
  62. when the looping process is complete.
  63.  
  64. The most important thing to remember when coding such a loop is
  65. to be sure that all the calls made between the repeat and the
  66. failing condition are deterministic.  Remember, when a call fails
  67. in Turbo Prolog, the backtracking mechanism returns to the last
  68. non-deterministic call made and attempts to REDO that call.
  69.  
  70. In the iterative loop design, it is very important that
  71. processing returns back to the top of the loop before attempting
  72. another cycle.  For this reason, we have coded a deterministic
  73. version of the predicate retract(), (the built-in predicate
  74. retract() is non-deterministic by definition).
  75.