writing 'remove' function in prolog
Posted
by Adrian
on Stack Overflow
See other posts from Stack Overflow
or by Adrian
Published on 2010-04-01T03:31:17Z
Indexed on
2010/04/01
3:33 UTC
Read the original article
Hit count: 382
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?
© Stack Overflow or respective owner