Add two more occurrences using prolog

Viewed 505

I have a list [a, b, a, a, a, c, c] and I need to add two more occurrences of each element.

The end result should look like this:

[a, a, a, b, b, b, a, a, a, a, a, c, c, c, c]

If I have an item on the list that is the same as the next item, then it keeps going until there is a new item, when it finds the new item, it adds two occurrences of the previous item then moves on.

This is my code so far, but I can't figure out how to add two...

dbl([], []).
dbl([X], [X,X]).
dbl([H|T], [H,H|T], [H,H|R]) :- dbl(T, R).
4 Answers
Related