简体   繁体   中英

Prolog predicate

Hello I am a newbie to Prolog with basic question. I would like to know why these Prolog predicates daughter_of/2 and son_of/2 do not work as I want them to. For example, if I ask

16 ?- daughter_of(alice, X).
true.

(Why true? What predicate would I need to get Victoria and Albert?)

Thank you.

male(albert).
male(edward).

female(alice).
female(victoria).

parents(edward, victoria, albert).
parents(alice, victoria, albert).

sister_of(X,Y):-
    female(X),
    parents(X,M,F),
    parents(Y,M,F).

brother_of(X,Y):-
    male(X),
    parents(X,M,F),
    parents(Y,M,F).

daughter_of(X,Y):-
    female(X),
    parents(X,M,F).

son_of(X,_Y):-
    male(X),
    parents(X,M,F).

The problem is the definition of your doughter_of predicate.

daughter_of(X,Y):-
female(X),
parents(X,M,F).

It should be

daughter_of(X,Y):-
female(X),
parents(X,Y,_).

daughter_of(X,Y):-
female(X),
parents(X,_,Y).

So that the Y parameter is passed on to the parents predicate. Otherwise it would not be used (and even gives me a warning when loading the file). Then prolog could only tell you that X is female and X has parents, but the actual parents would be discarded because M and F are no output parameters of your predicate.

Notice that i have defined the daughter_of predicate twice to work with the father as well as with the mother.

The same applies to son_of.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM