← All chapters
CHAPTER 03

Questions & Variables

Let Prolog search for answers.

Use → to advance · Esc for slide overview

Variables ask for unknowns

likes(ada, tea).
likes(ben, coffee).
likes(ada, music).

?- likes(ada, What).

Variables begin with an uppercase letter or underscore.

One question, multiple answers

?- likes(ada, What).
What = tea ;
What = music.

The semicolon is typically entered by the user to ask for another solution.

Variables connect goals

?- likes(Who, tea),
   likes(Who, music).
Who = ada.

One variable must refer to the same value in both goals.

A question for you

With the facts shown earlier, what does this query ask?

?- likes(Person, coffee).

It asks for each Person who likes coffee.

You are thinking in Prolog

Write facts. Define relationships. Ask questions.

More chapters coming soon

Return to all chapters →