I'm studying this new concept of logical programming with Prolog. I've worked with cuts, fails, and now I'm at the topic of lists. I have the following task to do:
-Write a function evenmember(Element, List)
, which will show if an element is at an even position in a list. For example evenmember(X,[1,5,3,2])
will show consecutively the solutions X=5,X=2
.
I've been playing around for a while and have written the following:
evenmember(_, []) :- !, fail.
evenmember(_, [_]) :- !, fail.
evenmember(X, [_, X]) :- !.
evenmember(Elem, List):-
[_, Elem1|Tail] = List,
Elem is Elem1,
evenmember(Elem1, Tail).
The only way to show the results that worked for me was to introduce write function, but it does not work for the task. As far as I remember, we cannot use Elem
and List
directly, so I'm kind of confused. I ask for assistance and I'm going to be grateful for your help =)