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

  1. /*
  2.    Turbo Prolog 2.0, Answer to first Exercise on page 203.
  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.   odd_list ( integerlist, integerlist )  
  13.  
  14. Clauses
  15.  
  16.   odd_list([], []) :- !.      % If the input list is empty, 
  17.                               % the output must be empty.
  18.  
  19.   odd_list([H|T1], [H|T2]) :-
  20.       1 = H mod 2, ! ,      % Is the head odd? 
  21.       odd_list(T1, T2).     % If so, add it to the head 
  22.                             % of the output list.
  23.  
  24.   odd_list([_|T1], T2) :-     % If the head is not odd,
  25.       odd_list(T1, T2).     % try the rest of the list!
  26.       
  27.   get_integer_list([Int|T]) :-
  28.       write("Enter an integer: ") ,
  29.       readint(Int), ! ,
  30.       get_integer_list(T).
  31.   get_integer_list([]).
  32.   
  33. Goal
  34.   makewindow(1,2,3," Odd List ",0,0,25,80) ,
  35.   write("Enter the integers to make a list.\n",
  36.         " (Enter a non-integer to end list.)\n\n") ,
  37.   get_integer_list(List),
  38.   odd_list(List, Odds) ,
  39.   write("\nThe whole list is: ",List,"\nThe odd list is  : ",Odds) ,
  40.   write("\n\nPress any key...") ,
  41.   readchar(_).