writing 'remove' function in prolog
- by Adrian
I am desperately trying to create a remove function, that will simply remove all items that equal to X from a list. After many changes, this is my code so far:
remove([], X, L1). /* when the source list is empty, stop*/
remove([X|T], X, L1) :- remove(T, X, L1). /* when first element in the list equals X, don't append it to L1 */
remove([H|T], X, L1) :- remove(T, X, [H|L1]). /*when first element in the list doesn't equal X, append it to L1 */
when running on
remove([1,2,3,4,5], 3, R).
it returns two trues and nothing else. Anyone has any idea what I'm doing wrong?