I am very new to Prolog programming. Any help is greatly appreciated. I have the following program,
bigger(elephant, horse).
bigger(horse, donkey).
bigger(donkey, dog).
bigger(donkey, monkey).
is_bigger(X, Y) :- bigger(X, Y).
is_bigger(X, Y) :- is_bigger(Z, Y), bigger(X,Z).
On running the query,
?- is_bigger(A, donkey)
I get the following output,
A = horse ;
A = elephant ;
ERROR: Out of local stack
While I do somewhat understand how A = horse and A = elephant, I am having a difficult time understanding why it's recursing infinitely (I used the builtin trace trace predicate but couldn't understand it after A = elephant ).
Thank you.
is_bigger(X, Y) :- is_bigger(Z, Y), bigger(X,Z).
The above line is the one causing the out of local stack message. You're calling "is_bigger" again which calls "is_bigger" again and so on, recursively
Your input: is_bigger(A, donkey)
First, you want to find something that is bigger than the donkey or/and something that is bigger than the thing that's bigger than the donkey.
Therefore:
is_bigger(X, Y) :- bigger(Z, Y), bigger(X,Z).
Why?
Y binds to donkey.
Z bind to horse.
As a last step, you're looking for bigger(X, horse)
Does that make sense?
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.