Ahmed Gad
Ahmed Gad

Reputation: 866

SWI-Prolog: Return all values except one

For the following SWI-Prolog program, I want to create a predicate that returns all values except a single one.
For example, return all males except ahmed.
How can I do that?

male(ahmed).
male(mohamed).
male(ali).
male(samir).
male(khalid).

Upvotes: 0

Views: 1172

Answers (1)

max66
max66

Reputation: 66210

Not sure to understand.

Do you want a predicate that return one single name of a (no Ahmed) male and, recalling it, via backtraching, another name, and another one... ?

I suppose that you can simply write

noAhmed(M) :-
  male(M),
  M \= ahmed.

Or do you want a predicate that return a list with all the (no Ahmed) male names?

In this case, you can write

noAhmedList(L) :-
  findall(M, (male(M), M \= ahmed), L).

Upvotes: 2

Related Questions