Search code examples
prologimmutability

Prolog - Manipulating the same list for several times


I am new to Prolog and trying to modify a list by deleting elements by indices.

For example:

remove(1,[1,2,3],Z).  
Z is [1,3]. 

However, when I try to run this predicate again, the list is sent as [1,2,3].

As far I searched, I think it cant be implemented in Prolog Is that true ?

And if its true, what alternatives I can try ?

I have been stuck in it for a long time and really any help is appreciated.


Solution

  • Using the predefined predicates nth0/3 and select/3

    ?- nth0(1,[1,2,3],Element),select(Element,[1,2,3],Z).
    Element = 2,
    Z = [1, 3] ;
    false.