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

  1. /*
  2.    Turbo Prolog 2.0 Chapter 8, Example Program 3
  3.    
  4.    Copyright (c) 1986, 88 by Borland International, Inc
  5.    
  6. */
  7.    
  8. domains
  9.    list = integer* /* or whatever type you want to use */
  10.  
  11. predicates
  12.    length_of(list, integer, integer)
  13.  
  14. clauses
  15. /* * * * * * * * * * * * * * * * * * * * * * *
  16.  * If the list is empty, bind the second arg *
  17.  *   (the result) to the third (the counter).*
  18.  * * * * * * * * * * * * * * * * * * * * * * */
  19.    length_of([], Result, Result).
  20.  
  21. /* * * * * * * * * * * * * * * * * * * * * * * * * * *
  22.  * Otherwise, add 1 to counter and recurse, binding  *
  23.  * Result in this invocation to Result in the next.  *
  24.  * * * * * * * * * * * * * * * * * * * * * * * * * * */
  25.    length_of([_|T], Result, Counter) :-
  26.       NewCounter = Counter + 1,
  27.       length_of(T, Result, NewCounter).
  28.  
  29. goal
  30.    length_of([1, 2, 3], L, 0), /* start with Counter = 0 */
  31.    write(L), nl.
  32.