I have a hard time understanding member + predicate call. For example when I have a predicate like this:
predicate(List) :- member(X, List), someCalculations(X).
And let's suppose that the predicate someCalculations(X)
sometimes returns true and sometimes false, or just false/true. Will it stop for the first true returned or when all the X
members from List return true ? And when will it fail, when it encounters fail once, or when all the List members failed ?
Predicate calls in Prolog don't "return false/true"; they succeed (optionally binding variables) or fail. And they can succeed repeatedly.
The answer to your question depends on how you call predicate
. Assuming it's a goal by itself, and you pass it a list containing no unbound variables, e.g. ?- predicate([a,b,c,d])
, this will happen:
member(X,[a,b,c,d])
first succeeds with X=a
. Then someCalculations(a)
gets called.
If it succeeds, predicate([a,b,c,d])
call also succeeds, without binding any variables. You can stop, or you can ask for more results in which case...
If it fails, Prolog backtracks and...
... X
gets bound to b
, someCalculations(b)
is called. Etc.
After step 4 (with d
), member
call fails so predicate
call does as well (someCalculations
doesn't get called).