home *** CD-ROM | disk | FTP | other *** search
Prolog Source | 1987-08-11 | 1.2 KB | 31 lines |
- /* Listing 1 - Prolog module to find the mean using tail recursion
- elimination */
-
- domains
- real_list = real*
-
- database
- answer(real) /* Create storage for answers. */
-
- predicates
- process(real_list)
- mean(real_list,real,integer)
-
- clauses
- process(List):-
- N=0, /* Initialize Count to 0. */
- S=0, /* Initialize temp storage var */
- mean(List,S,N), /* Pass "mean" the list and vars. */
- answer(Answer), /* Get answer from storage. */
- write(Answer).
-
- mean([H|T],S,N):- /* "mean" recursively processes */
- Y=H+S, /* the list --adding each member */
- N2=N+1, /* to the sum of the others and */
- mean(T,Y,N2). /* keeping track of the no of */
- /* members. */
- mean([],S,N):- /* When the list is empty, */
- Z=S/N, /* divide the total by the count */
- assert(answer(Z)). /* and store the answer. */
-
-