swi Prolog - Error arguments not sufficiently Instantiated -
I'm new to Prolog and when I query sortedUnion ([1,1,1,2,3, 4, 4,5], [0,1,3,3,6,7], [0,1,2,3,4,5,6,7]).
I get an error
Exception: (7) Unify ([_G114, _G162, _G201, _G231, _G243], [_G249, _G297, _G336, _G357, _G369] ], [0, 1, 2, 3, 4, 5, 6, 7])?
So I hope someone can tell me why my code is wrong and why is it wrong?
% undup (L, U) keeps properly when the UL can be obtained, destroy the repeating components of the same element ([], []). UPDATE ([x | xs], [_ | B]): - Remove (x, x, k), apu (k, b). remove(_,[],[]). Remove (Y, [Y | T], D): - Remove (Y, T, D). Remove (Y, [S | T], [SR]): - No (Y = S), Remove (Y, T, R). % Holds SortedUnion (L1, L2, U), when U is an example of each element of L1 and L2 sortidunes ([H.T.T], [SR], [F | B]): [H. T.], N.), Apup ([SR], M), united (N, M, [F | B]). Unite ([], [], [] unite ([X], [], [X]) unify ([], [X], [X] unify ([H.T.T], [ SR], [X | Ax]]: - S = H, X, is united (T, R, XS). Unite ([H. T], [SR], [X | Ax]]: - H & LT; S, X is H, united (T, [SR], XS). Unite ([H. T], [SR], [X | Ax]]: - S & LT; H, XS, united ([H. T], R, XS).
First Advice: Try to keep your code as simple as possible Your code (Which certainly works)
Sortedion (A, B, S): - App (A, B, C), Sort (C, S)
But definitely it is instructive to try to solve themselves. Anyway, try to avoid unnecessary complications. Sortedunian (A, B, S): - Apup (A, N), Apup (B, M), united (N, M, S).
This is equivalent to your code, just simple, because A = [H | T]
and so on.
then incomplete / 2:
1? - Absent ([1,1,1,2,3,4,4,5], L) L = [_G2760, _G2808, _G2847, _G2877, _G2889]; false.
Obviously, the criminal who does not expect what you expect is an infinite war, actually works:
Apup ([], [] ). UPDATE ([x | xs], [x | b]): - Remove (x, xs, k), apup (k, b). 2? - Absent ([1,1,1,2,3,4,4,5], L) L = [1, 2, 3, 4, 5]; false.
Now, unite / 3 First of all, / 2 has been exploited. It introduces mathematics, then plain integration is sufficient here: X = S
.
Then the matters of the base are hardened to work, where the length of the lists is greater than 1. Then, the simpler code should work better:
unite ([], [], []). Unite (X, [], X). Unite ([], X, X). ...
Also, note that the first section is useless, already covered by both (second) and third sections.
Comments
Post a Comment