home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / nvdc87 / lcv1n1 / mean2.pro < prev    next >
Text File  |  1987-08-11  |  920b  |  33 lines

  1. /* Listing 6 -  Mean in PROLOG without tail recursion elimination */
  2.  
  3. domains
  4.    real_list = real*
  5.  
  6. database
  7.    answer(real)
  8.    data(integer,real_list)
  9.  
  10. predicates
  11.    process(real_list)
  12.    mean(real_list,real,integer)
  13.     
  14. clauses
  15.    process(List):-
  16.       N=0,
  17.       S=0,
  18.       mean(List,S,N),   /* Call Turbo PROLOG function.        */
  19.       answer(Answer),    /* to calculate the mean.             */
  20.       write(Answer).    /* Get the answer from the answer and */
  21.                         /* report.                            */
  22.  
  23.    mean([],_,_).
  24.    mean([H|T],S,N):-
  25.       Y=H+S,
  26.       N2=N+1,
  27.       mean(T,Y,N2),
  28.       T = [],            /* These lines force mean to     */
  29.       Z=Y/N2,         /* remember an address each time */
  30.       assert(answer(Z)). /* it calls itself recursively.  */
  31.    mean([_|_],_,_).      /* We need this line to make     */
  32.              /* mean always succeed.          */
  33.