home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / prolog / 2340 < prev    next >
Encoding:
Internet Message Format  |  1993-01-08  |  2.0 KB

  1. Path: sparky!uunet!mcsun!uknet!edcastle!aiai!ken
  2. From: ken@aiai.ed.ac.uk (Ken Johnson)
  3. Newsgroups: comp.lang.prolog
  4. Subject: Re: Making up sums of money from coins
  5. Message-ID: <8134@skye.ed.ac.uk>
  6. Date: 8 Jan 93 17:32:49 GMT
  7. References: <8088@skye.ed.ac.uk>
  8. Followup-To: comp.lang.prolog
  9. Organization: William's Wonderful Wonky Widget Warehouse
  10. Lines: 64
  11.  
  12.  
  13. The following code works, but I have not a clue how to generalise it. 
  14. At least, I have not chanced upon a case where it does not give the best
  15. answer.  The reason for the op/3 call will become clear in a minute,
  16. although those over thirty will recognise it on sight:
  17.  
  18. :-  op(500, xf, '/-').
  19.  
  20. make(A/-,C) :-
  21.     Sum is 12 * A,
  22.     make(Sum,C).
  23.  
  24. make(A/B,C) :-
  25.     Sum is A * 12 + B,
  26.     make(Sum,C).
  27.  
  28. make(0,[]).
  29.  
  30. make(Sum,[Name|Rest]) :-
  31.     Sixpences is Sum//6,            % If the number of sixpences
  32.     coin(Name,Value),            % in the sum is an exact
  33.     Value =< Sum,                % multiple of 4, then do not
  34.     \+ (    0 is Sixpences mod 4,        % allocate a half crown.
  35.         Value is 30            % No clue how to generalise
  36.        ),                    % this to other currencies.
  37.     Residue is Sum - Value,            % The LCM of 4 and 5 is 20 so
  38.     make(Residue,Rest).            % 10/- is dispensed as a
  39.                         % ten shilling note, not as
  40.                         % five florins (wrong)
  41. % Coins; must be in order of decreasing value.    
  42.  
  43. coin('ten shilling note',120).
  44. coin('half-crown',30).
  45. coin(florin,24).
  46. coin(shilling,12).
  47. coin(sixpence,6).
  48. coin('threepenny bit',3).
  49. coin(penny,1).
  50.  
  51. % Sample Run
  52.  
  53. This program understands the traditional way of writing `four shillings'
  54. as 4/- and four shillings and elevenpence as 4/11. 
  55.  
  56.  ?- make(4/-,X).
  57.      X=[florin,florin]
  58.  
  59.  ?- make(4/6,X).
  60.      X=[half-crown,florin]
  61.  
  62.  ?- make(5/-,X).
  63.      X=[half-crown,half-crown]
  64.  
  65.  ?- make(5/11,X).
  66.      X=[half-crown,half-crown,sixpence,threepenny bit,penny,penny]
  67.  
  68.  
  69. Ken Johnson
  70.  
  71. -- 
  72. Son, all the pretty, intelligent, healthy     #    Ken Johnson, AIAI,
  73. young women are taken. It's a basic rule of   #    80 South Bridge, Edinburgh
  74. the universe, and if you don't like it, go    #    Tel 031-650 2756 
  75. somewhere else.        -- my dad  1906-1992   #    Fax 031-650 6513
  76.