1

i done my prolog coding and no error when run it. but still cannot get the output i want. so i want to ask where is the problem. my coding is like bellow

even_member(X, [X|_]).

even_member(X, [_|Tail]) :-
    0 is X mod 2,
    write(X), nl,
    even_member(X, Tail).

the output i need it is when key in even_member(2,[1,2,3,4,5,6]). will get output like bellow

X=2

X=4

X=6

yes

Can anyone help me tell me what's the problem for my coding. Thank you.

1
  • Before asking more questions, you should accept answers of old questions. Also your question doesn't make much sense. Did you mean even_member(X,[1,2,3,4,5,6]) instead of even_member(2,[1,2,3,4,5,6])?
    – Cephalopod
    Oct 12, 2010 at 19:32

1 Answer 1

1

I can suggest:

even_member(X, List) :-
    member(X, List),
    0 is X mod 2.

Which, when evaluating, gives:

?- even_member(X,[1,2,3,4,5,6]).
X = 2 ;
X = 4 ;
X = 6 ;
false.

Otherwise, if you wanted behaviour that simply just prints all the values out in one go, you can try:

even_member(List) :-
    member(X, List),
    0 is X mod 2,
    write(X), nl,
    fail ; true.

Which, when evaluating, we get:

?- even_member([1,2,3,4,5,6]).
2
4
6
true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.