0
votes

I am student and I have a problem with amzi prolog. We learned at class that there is a predicate that returns true if an item is a member in a list:

member(1,[1,2,3]) should return true.

Yet I get false.

Am I doing something wrong?

please help.

regards, Id

2
Do you expect a detail answer on this 'Am I doing something wrong?' ? - CapelliC
@Idob it's very unlikely that member/2 doesn't work. It would be like saying (1 + 2) == 3 wasn't working in C. Perhaps you could provide more context, describing where you are actually querying member(1, [1,2,3]). Is it inside another predicate you've written? Are you just typing that in exactly at an Amzi prolog interpreter prompt (not forgetting the terminating period .!). You have really provided very little information. - lurker
This is a trap for the unwary. The Listener starts in Amzi! Prolog with member/2 undefined, and undefined predicates simply return False (unlike with SWI-Prolog reporting undefined predicates as errors). See more details in my Answer. - hardmath
Just wondering, why are you using Amzi Prolog? There are several implementations that should be preferred in almost any context: SWI-Prolog, YAP, GNU-Prolog, SICSTus.... - user1812457
I am familiar with eclipse and I find amzi's debugger the closet there is to Java debugger - so it is easier for me. Is there an easy way to debug with SWI? I couldn't find it. - Ido Barash

2 Answers

2
votes

Amzi! Prolog has a built-in predicate is_member/2 which is (semi)deterministic (does not backtrack), and it has the standard member/2 that must be loaded via the list.pro library.

Since the definition is a two-liner, I usually just put the definition of member/2 in my source for a project as needed.

0
votes

Amzi! Prolog's built-in predicate is_member/2, per its documentation, is deterministic and simply tests membership. It won't return all members of the list on backtracking:

is_member/2 is a restricted version of the classic member/2 predicate (in the LIST.PLM library) than [sic] can be used for fast testing if Term is a member of List. It uses a strong unify (==) for testing the element. It cannot be used to backtrack through the various members of a list. The definition is equivalent to:

is_member(X, [Y|_]) :- X == Y, !.
is_member(X,[_|Z]) :- is_member(X,Z).

As @hardmath noted in another answer, the classic member/2 is part of Amzi! Prolog's list library. However, Prolog is [mostly] written in Prolog, so you can easily roll your own member/2 having to import the list library:

member( X , [X|_]  ) .
member( X , [_|Xs] ) :- member(X,Xs) .