How to do it in Prolog?

I am new to Prolog.

Using this basic database structure, I thought I could query the database to find out

  • who eats fish?
  • What do whales eat?

    eats(Horse, grass).  
    eats(Monkey, banana).  
    eats(Whale, fish). 
    

I would not want to change this database setting (if possible). I used the following queries with corresponding undesirable results:

Here I tried to ask: "Who eats fish?"

?- eats(X, fish).
true.

Here I tried to ask: "What do the whales eat?"

?- eats(Whale,X).
X = grass ;
X = banana ;
X = fish.
+3
source share
2 answers

Your queries are correct, this is an incorrect fact database. Atoms need to start with a lowercase letter (or indicate). You started the Horse, Monkey, Whale with capital letters, so they are variables (and correspond to something). Therefore, your current database is equivalent to:

eats(X, grass).
eats(X, banana).
eats(X, fish).
+9

All Articles