swi-prolog progam "no change given" not quite returning denominations properly -
If I reduce 'abortion' in line 3, then the program estimates the required output on the screen like :
? - Salary with the largest _name (37, P) 25; Remaining = 12 pay with 10; Salary with balance = 2 1; Pay with Balance = 1 1; Remaining = 0 ended% execution aborted? -
However, I need a 'pure' return-solution-when-hit-semicolon or return-in-list variation. ("Trace." Did not provide any clue).
The best I can not get (3) with "3%" with 'blank'):
? - Make a payment with the largest _Denom (13, P) 10; Salary with Balance = 3 1; Salary with balance = 2 1; Pay with Balance = 1 1; Balance = 0 ends p = 10; Make a payment with 5; Pay with balance = 8 5; Salary with Balance = 3 1; Salary with balance = 2 1; Pay with Balance = 1 1; Balance = 0 ends p = 5; Make a payment with 1; 7 wages with balance = 7; Salary with balance = 2 1; Pay with Balance = 1 1; Balance = 0 ends p = 5; Make a payment with 1; Balance = 6 pay 5; Pay with Balance = 1 1; Balance = 0 ends p = 5; Make a payment with 1; Payment with balance = 5 5; Balance = 0 ends p = 5; Make a payment with 1; Salary with balance = 4 1; Salary with Balance = 3 1; Salary with balance = 2 1; Pay with Balance = 1 1; Balance = 0 ends P = 5% IT goes on this and so on !! XXXXXXXXXXXXX
Here's my code:
% XXXXXXXXXXXXX not given XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX accepted ([100,50,25,10,5,1]). % Pre-ordered (largest list) biggest _demonem (0, _): - Written ('Finnish'). %, Abortion. %Stop!!! Scary! Big_denom (N, D): - N \ = 0, Accept (x), Member (D, X), D = & lt; N, the remaining ND, write ('pay with'), write (d), write ('; remaining ='), resell (balance),% balance \ = 0,% gives more output, for some reason For, not least the largest _Denom (balance, D2) salary (N): -% denom's print list which connects to N; Findall will use writeln ('not implemented yet')
Any advice will be appreciated!
You want to write for you at the top level and you accept accept /
can get rid of the list and keep them in the form of facts. Accept (100)
Accept (50). Accept (25). Accept (10). Accept (5). Accept (1). Return_change (N, C): - Integer (N), N & gt; 0, return_ project_1 (n, c) Return_change_1 (0, []): -! Return_change_1 (n, [xr | change]): - accept (x), x = & lt; N, rn is - x, return_weight (r, change).
This is actually what you need to do.
? - Return_change (37, C),! C = [25-12, 10-2, 1-1, 1-0].
Or, if you insist on output:
? - return_change (37, c),!, Forall (member (pr, c) format ("pay ~ d; balance ~ d ~ n", [p, r])). Pay with 25; Remaining 12 pay 10; Balance 2 pay 1; Balance 1 payout 1; Remaining 0 C = [25-12, 10-2, 1-1, 1-0]
Remove the cut in the query to get more resolution.
As @mat says, call foo,!
can be expressed by using more decorated once / 1
, like this: one time (foo)
Comments
Post a Comment