dimanche 23 mars 2014

Duplicate nth value from list


Vote count:

0




Fairly new to Prolog; I'm trying to implement a recursive rule duplicate_nth(N, L1, L2):- which takes a nth value N, a list L1 and duplicates the Nth value of L1 and returns it in a list L2.


Example: querying duplicate_nth(1, [2,3,4], X). should return X = [2,2,3,4].


My current code is:



duplicate_nth(N,[H|T],L2) :- % N is 1? Append the head of the list to the head and tail of the list
N = 1,
append([H],[H|T],L2).

duplicate_nth(N,H|T,L2) :-
N > 1,


which currently works if N = 1, it will not however work if N > 1 and I am unsure of how to proceed.




false

8,390

asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire