In the last lesson, you got a taste of Prolog facts and goals. Let's continue this now, with an example that might make you think "Hmmm...how did the computer do that?"
We'll use the likes fact again, but with a twist.
likes(ellen,tennis). likes(bill,X):-likes(tom,X).
Move the mouse over a dotted box for more information.
Reading likes(bill,X) :- likes(tom,X). means: bill will like item X if tom likes item X. So when Prolog tries to see if bill likes item X, it first has to determine if tom also likes item X (which is can determine from the raw facts).
Now you try. Before running this code, look at it and see if you can tell if bill likes baseball or not. How do you know?
Type your code here:
See your results here:
Go ahead and run this code as is and see what Prolog will do. Can you tell from the likes facts alone that bill likes baseball? And think again: how would you implement
such a code in Lua? Would it be more complicated?
You can also try goals like likes(Person,tennis).
When done, add the line likes(john,X) :- likes(ellen,X), likes(mark,X). and change the goal to see if john likes tennis. The comma means "and." So this fact says "john likes X if ellen likes X and mark likes X."
Share your code
Show a friend, family member, or teacher what you've done!