Search code examples
prolog

DELETE AN ELEMENT OF A LIST IN PROLOG


I need to delete an element of a list in prolog, but it only work when you put the list in the consult for example: eliminar(1,[1,2,3], X). it returns X=2,3, but when I wanna delete an element of the list "aves" it gives me false eliminar(loro,aves,X) it returns false.

aves([aguila, paloma, loro, canario]).
% Predicado para eliminar un elemento de una lista
eliminar(X,[X|Xs], Xs).
eliminar(X,[Y|Ys] , [Y|Zs]) :- eliminar(X,Ys,Zs).

I'm trying putting [aves], and it doesn't work


Solution

  • Only capital words are variables (_vars are special case) in prolog. When you write aves, it will not be substituted to anything else.

    If have declared, aves([...]). then to get that list back you have to query aves(X) which returns the list as the variable X.

    aves([aguila, paloma, loro, canario]).
    | ?- aves(X).
    X = [aguila,paloma,loro,canario]
    yes 
    

    So your query should be:

    | ?- aves(X), eliminar(loro, X, Y).
    X = [aguila,paloma,loro,canario]
    Y = [aguila,paloma,canario] ? 
    yes