I need some help. I searched through the database and I found one question already asked about this example, but the answers didn't really help me, so I thought to post my own question.
The task is to pack consecutive duplicates of list elements into sublists:
% ?- pack([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
% X = [[a,a,a,a],[b],[c,c],[a,a],[d],[e,e,e,e]].
Here is what I got:
pack([], []).
pack([X], [[X]]).
pack(Liste, Ergebnis):-
Liste = [H, T|TS],
H \= T,
pack([T|TS], Ergebnis1),
append([[H]], Ergebnis1, Ergebnis).
pack([H, H|HS], Ergebnis):-
pack([H|HS], Ergebnis1),
append([H|HS], Ergebnis1, Ergebnis).
The first case works really well (case where H \= T). The second one doesn't, and I really don't know why. Could someone please help me and explain the problem according my solution?
Thanks
b
a consecutive duplicate in[a,a,a,a,b,c,c,a,a,d,e,e,e,e]
? – falseb
there? – false