How to do this in Prolog?
I'm new to Prolog.
Using this basic 'database' structure, I thought I would be able to query the database to find out
- who eats fish?
what do Whales eat?
eats(Horse, grass). eats(Monkey, banana). eats(Whale, fish).
I'd like to not change that database setup (if possible). I was using the following queries with the respective unwanted results:
Here, I was trying to ask, 'who eats fish?'
?- eats(X, 开发者_JAVA技巧fish).
true.
Here, I was trying to ask, 'what do whales eat?'
?- eats(Whale,X).
X = grass ;
X = banana ;
X = fish.
Your queries are correct, it's your fact database which is wrong. Atoms need to start with a lowercase letter (or be quoted). You started Horse, Monkey, Whale with uppercase letters, so they are variables (and match anything). Hence your current database is equivalent to:
eats(X, grass).
eats(X, banana).
eats(X, fish).
The objects in the facts must begin in lower case, or wrapped in quotes. In this case they would be atoms. In your case, Prolog thinks of them as variables.
http://www.cse.unsw.edu.au/~billw/cs9414/notes/prolog/intro.html#facts
精彩评论