Prolog list operation
Here's my problem (by example since that's quicker):
?- enum_list([alpha, beta, gamma, beta, beta, delta, epsilon, 开发者_StackOverflow中文版alpha], L).
L = [alpha1, beta1, gamma, beta2, beta3, delta, epsilon, alpha2].
The problem is simple when I am allowed to reorder the list (just sort the list, group same elements into lists, enum the lists if they are longer than 1). But I want to keep the order. Any ideas?
How about:
enum_list(L, E):-
enum_list(L, E, [], _).
enum_list([], [], B, B).
enum_list([X|Tail], [Y|NTail], B, NB):-
select(X-C, B, MB),
succ(C, C1),
atom_concat(X, C1, Y),
!,
enum_list(Tail, NTail, [X-C1|MB], NB).
enum_list([X|Tail], [Y|NTail], B, NB):-
enum_list(Tail, NTail, [X-1|B], NB),
(member(X-1, NB) -> Y=X ; atom_concat(X, 1, Y)).
It iterates through the list and keeps a set of count of occurrences of each item so it knows when and what to append for each item to get the name.
精彩评论