Your forth clause tries to append [H|HS]
to the result, which is incorrect, because [H|HS]
is the tail of the original list. You can make it a lot simpler -
pack([H, H|HS], [[H|TFR]|TR]):-
pack([H|HS], [TFR|TR]).
Essentially, it says that when the first two entries are the same in the input, the first entry (i.e. H
) needs to be pre-pended to the first entry of the output list produced by recursive invocation of the pack
rule.
Note that the third clause can be simplified as well by replacing the Liste
parameter which you "crack" right away with the "cracked" version "inlined" into the header of the clause, and doing the same to the output variable Ergebnis1
. The final version should look like this:
pack([H, T|TS], [[H]|TR]):-
H \= T,
pack([T|TS], TR).