For Programmers: Free Programming Magazines  


Home > Archive > Prolog > May 2006 > how to delete Nth element from a list









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to delete Nth element from a list
erhan

2006-05-11, 4:05 am

hi,

I want to delete Nth element from a list. How can I do this?
I mean, I want a predicate delNthFromList(N, List, NewList) which
deletes Nth element from List and construct a NewList.

for ex.,
delNthFromList(3, [a,b,c,d], L). should return L = [a,b,d]...

thanks in advance...

knd

2006-05-11, 8:01 am

I think this would be applied.

delNthFromList(1,[X|L],L) :- !.
delNthFromList(N,[X|L1],[X|L2]) :-
N1 is N - 1,
delNthFromList(N1,L1,L2).

--shinichi

michael.goodrich@gmail.com

2006-05-11, 8:01 am


erhan wrote:
> hi,
>
> I want to delete Nth element from a list. How can I do this?
> I mean, I want a predicate delNthFromList(N, List, NewList) which
> deletes Nth element from List and construct a NewList.
>
> for ex.,
> delNthFromList(3, [a,b,c,d], L). should return L = [a,b,d]...
>
> thanks in advance...



removeNth([H|T],1,T).
removeNth([H|T],N,[H|X]) :- N1 is N-1, removeNth(T,N1,X).

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com