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

  1. /*
  2.    Turbo Prolog 2.0, Answer to third Exercise on page 193.
  3.    
  4.    Copyright (c) 1986, 88 by Borland International, Inc
  5. */
  6.  
  7. Domains
  8.   integerlist = integer*
  9.  
  10. Predicates
  11.   get_integer_list ( integerlist )
  12.   get_list_sum ( integerlist, integer, integer )
  13.   sum_list ( integerlist, integer )  
  14.  
  15. Clauses
  16.   sum_list(List, Sum) :-
  17.       get_list_sum(List, 0, Sum).
  18.  
  19.   get_list_sum([], X, X) :- !.
  20.   get_list_sum([Num|T], Temp, Sum) :-
  21.     Temp_sum = Temp + Num ,
  22.       get_list_sum(T, Temp_sum, Sum).
  23.       
  24.   get_integer_list([H|T]) :-
  25.       write("Enter an integer: ") ,
  26.       readint(H), ! ,
  27.       get_integer_list(T).
  28.   get_integer_list([]).
  29.  
  30. Goal
  31.   makewindow(1,2,3," Sum of an Integerlist ",0,0,25,80) ,
  32.   write("Please enter the integers which you wish to sum.\n" ,
  33.         "  (Enter a non-integer to stop.)\n\n") ,
  34.   get_integer_list(List) ,
  35.   sum_list(List, Sum) ,
  36.   clearwindow ,
  37.   write("The sum of the list is: ",Sum,"\n").