home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / nvdc87 / lcv1n1 / mean.pro < prev    next >
Text File  |  1987-08-11  |  1KB  |  31 lines

  1. /* Listing 1 - Prolog module to find the mean using tail recursion 
  2.                elimination */
  3.  
  4. domains
  5.    real_list = real*          
  6.  
  7. database
  8.    answer(real)              /* Create storage for answers. */
  9.  
  10. predicates
  11.    process(real_list)
  12.    mean(real_list,real,integer)
  13.  
  14. clauses
  15.    process(List):-
  16.       N=0,                      /* Initialize Count to 0.         */
  17.       S=0,                      /* Initialize temp storage var    */
  18.       mean(List,S,N),           /* Pass "mean" the list and vars. */
  19.       answer(Answer),           /* Get answer from storage.       */
  20.       write(Answer).            
  21.  
  22.    mean([H|T],S,N):-            /* "mean" recursively processes   */
  23.       Y=H+S,                    /* the list --adding each member  */
  24.       N2=N+1,                   /* to the sum of the others and   */
  25.       mean(T,Y,N2).             /* keeping track of the no of     */
  26.                                 /* members.                       */
  27.    mean([],S,N):-               /* When the list is empty,        */
  28.       Z=S/N,                    /* divide the total by the count  */
  29.       assert(answer(Z)).        /* and store the answer.          */
  30.     
  31.