tags:

views:

38

answers:

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.

A: 

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.
sharky