In trying to better understand prolog, lists and recursion as a whole I'm working my way through various simple tasks I've assigned to myself. Among others is removing double entries from a list.
I've defined a rule:
is_on(Item, [Ah|At]) :- Ah = Item; is_on(Item, At).
This checks if 'Item' is on the list X or not. So I thought I could expand this to define a filter_double predicate as well:
filter_doubles([Ah|At], Result) :-
(not(is_on(Ah, At)) ->
Result = [Ah|Result]
;
filter_doubles(At, Result)
).
This made perfect sense to me: if Ah doesn't occur in the rest of the list (its tail), then add a to the front of result using list construction, otherwise recurse over the rest of the list. Apparently Prolog thinks otherwise:
47 ?- filter_doubles([1,2,3,3,4,2,1,1], Z).
Z = [3|**].
Am I thinking too imperative on this?
is_on(Item, [H|T]) :- Item = H; is_on(Item, T).
oris_on(X,L) :- memberchk(X,L).
– Fred Foofilter_double
orfilter_doubles
? – Fred Foo