Filter List in Prolog
I am developing an application written in Prolog. I get to the point where I want to filter the list of items and remove those that do not meet certain constraints or conditions while maintaining the original order of the list. I thought the best way to do it would be something like this:
filter([],Filtered).
filter([L|List],[F|Filtered]) :-
/* Conditions are met: bypass the element to the filtered list */
check_conditions(L),
filter(List,Filtered).
filter([L|List],Filtered) :-
/* Conditions are not met: do not include the element in the filtered list */
filter_aborts(List,Filtered).
Before accepting it as a solution to my problem, I wanted to try (independently), so I compiled and ran my code (SWI-Prolog) and tested several situations. When I enter a Prolog query using a hardcoded list (or whatever you want to call it), I get something like this:
?- filter([id01,id02,id03,id04,id05,id06],F). F = [id03, id05, id06|_G1024] .
The list is definitely being filtered, but I get this instance name " _G1024
" at the end of it. I understand this is happening because F is not being created, but I don't know what the solution would be. Moreover, if I try to do something else, like injecting a list as an instance of a variable, then I get even weirder:
?- L=[id02,id03,id04,id05,id06]. L = [id02, id03, id04, id05, id06]. ?- filter(L,F). L = [] ; L = [id01], F = [id01|_G347] ; L = [id01, id01], F = [id01, id01|_G403] ... and so on.
Shouldn't Prolog variables be single assignment variables? Is my program really changing L
or am I not understanding it correctly? Some of this, since I'm new to Prolog, I would appreciate any comments regarding my Prolog -let say ...- "style".
source to share