Search code examples
prologdcg

Prolog function iscontained?


I'm trying to make a Prolog predicate iscontained/2: iscontained(List, Search) where it returns true. if the Search is listed within the given List, false. if not. And if it is a variable that is inputted, then it just returns that it equals each element in the list.

Example:

?- iscontained([a, b, c], a).

true.

?- iscontained([a, b, c], d).

false.

?- iscontained([a, b, c], A).

A = a;
A = b;
A = c;
false.

I need a shove in the right direction, not asking for a hand out, unless you know a quick way to do it. Any help is appreciated, thanks.


Solution

  • You will need to consider two cases. I'll leave the body of the rules up to you.

    • iscontained([A|Xs],A)
    • iscontained([X|Xs],A)

    [edited to remove reference to the empty list: the empty list contains nothing: if encountered, the predicate fails.]