Reputation: 1474
I wonder if it's possible to do something like the following in prolog
L = [1,2,3,4,5,_,_,7].
i want to do something like
getElements(L, R)
that returns R = [1,2,3,4,5,7]
and ignore the wildcards in the list
Upvotes: 0
Views: 103
Reputation: 60004
If your Prolog has library(apply), you can write
getElements(L, R) :-
include(nonvar, L, R).
or, a bit more compact
getElements(L, R) :-
exclude(var, L, R).
Otherwise, this should be a working predicate:
getElements([], []).
getElements([H|T], [H|R]) :-
nonvar(H), !,
getElements(T, R).
getElements([_|T], R) :-
getElements(T, R).
edit as highlighted by @false, the latter getElements/2 is buggy, due to matching with the anonymous variable in last clause. Here a correction
getElements([], []).
getElements([H|T], R) :-
var(H), !,
getElements(T, R).
getElements([H|T], [H|R]) :-
getElements(T, R).
Upvotes: 1