Skip to content

Instantly share code, notes, and snippets.

@trietptm
Created December 13, 2012 17:03
Show Gist options
  • Save trietptm/4277978 to your computer and use it in GitHub Desktop.
Save trietptm/4277978 to your computer and use it in GitHub Desktop.
Duplicate the elements of a list a given number of times. Example: ?- dupli([a,b,c],3,X). X = [a,a,a,b,b,b,c,c,c]
appendlist([], X, X).
appendlist([H|T], L1, [H|L2]) :- appendlist(T, L1, L2).
dupli([], M, []) :-!.
dupli([X], 1, [X]) :-!.
dupli([X], M, [X|L]) :- N is M - 1, dupli([X], N, L), !.
dupli([H|T], M, L1) :- dupli([H], M, H1), dupli(T, M, T1), appendlist(H1, T1, L1), !.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment