Prolog "or" operator, query

122,467

Solution 1

you can 'invoke' alternative bindings on Y this way:

...registered(X, Y), (Y=ct101; Y=ct102; Y=ct103).

Note the parenthesis are required to keep the correct execution control flow. The ;/2 it's the general or operator. For your restricted use you could as well choice the more idiomatic

...registered(X, Y), member(Y, [ct101,ct102,ct103]).

that on backtracking binds Y to each member of the list.

edit I understood with a delay your last requirement. If you want that Y match all 3 values the or is inappropriate, use instead

...registered(X, ct101), registered(X, ct102), registered(X, ct103).

or the more compact

...findall(Y, registered(X, Y), L), sort(L, [ct101,ct102,ct103]).

findall/3 build the list in the very same order that registered/2 succeeds. Then I use sort to ensure the matching.

...setof(Y, registered(X, Y), [ct101,ct102,ct103]).

setof/3 also sorts the result list

Solution 2

Just another viewpoint. Performing an "or" in Prolog can also be done with the "disjunct" operator or semi-colon:

registered(X, Y) :-
    X = ct101; X = ct102; X = ct103.

For a fuller explanation:

Predicate control in Prolog

Share:
122,467
Eogcloud
Author by

Eogcloud

Student Intern working with .NET technologies.

Updated on May 18, 2020

Comments

  • Eogcloud
    Eogcloud almost 4 years

    I'm working on some prolog that I'm new to.

    I'm looking for an "or" operator

    registered(X, Y), Y=ct101, Y=ct102, Y=ct103.
    

    Here's my query. What I want to write is code that will:

    "return X, given that Y is equal to value Z OR value Q OR value P"

    I'm asking it to return X if Y is equal to all 3 though. What's the or operator here? Is there one?