Вы находитесь на странице: 1из 4

# Prolog Tutorial 1

Prolog Tutorial 1
1. Consider the following database where ako stands for "a kind of" and isa stands for "is a":

ako(chair,furniture).
ako(chair,seat).
isa(your_chair,chair).
isa(you,person).
colour(wood,brown).
belongs_to(your_chair,you).

Suppose we now want to find out what colour your chair is. The database does not give us the colour of chairs directly, but
it does give us the colour of what things are made of. To solve the colour problem, we first find what your chair is made of,
and then find its colour.

## 2. Determine whether your chair is a seat.

?- isa(your_chair,X), ako(X,seat).

## 3. Determine whether your chair belongs to a person.

?- belongs_to(your_chair,X), isa(X,person).

## Consider the following database of carnivores and herbivores:

isa(carnivore,lion).
isa(carnivore,leopard).
isa(carnivore,bear).

isa(hunter,lion).
isa(hunter,leopard).
isa(hunter,bear).

isa(herbivore,elephant).
isa(herbivore,zebra).
isa(herbivore,wildebeeste).
isa(herbivore,bear).

isa(hunted,zebra).
isa(hunted,wildebeeste).

4. Is there an animal in the database which is a herbivore, such that this animal also hunts?

?- isa(herbivore,Animal), isa(hunter,Animal).

5. Is there an animal in the database which is a herbivore, but at the same time is not a hunted animal?

## http://www2.dcs.hull.ac.uk/NEAT/dnd/AI/prolog/tutorial1.html (1 of 2) [03/03/2010 09:33:35 a.m.]

Prolog Tutorial 1

## Now consider the following animal database:

animal(mammal,tiger,carnivore,stripes).
animal(bird,eagle,carnivore,large).
animal(mammal,hyena,carnivore,ugly).
animal(bird,sparrow,scavenger,small).
animal(mammal,lion,carnivore,mane).
animal(reptile,snake,carnivore,long).
animal(mammal,zebra,herbivore,stripes).
animal(reptile,lizard,scavenger,small).

## 6. Add a rule that writes a list of all the animals' names.

write_names :- animal(_,Name,_,_),write(Name),nl,fail.
write_names.

## write_characteristics :- animal(_,Name,_,Char),write(Name),write(' '),write(Char),

nl,fail.
write_characteristics.

## 8. Add a rule that writes a list of those which are mammals.

write_mammals :- animal(mammal,Name,_,_),write(Name),nl,fail.
write_mammals.

9. Using the Prolog standard predicates name/2 and member/2 write a rule indef_article which takes an atom as its
first argument and returns the indefinite article (either a or an) as its second argument, correctly matching the atom; i.e. if
the atom begins with a vowel it should be an, if it begins with a consonant it should be a, as follows:

?- indef_article(apple,X).
X = an

?- indef_article(chair,X).
X = a

indef_article(Word,an):- name(Word,[N|_]),
name(aeiou,Vowels),
member(N,Vowels).
indef_article(_,a).

## http://www2.dcs.hull.ac.uk/NEAT/dnd/AI/prolog/tutorial1.html (2 of 2) [03/03/2010 09:33:35 a.m.]

Prolog Tutorial 2

Prolog Tutorial 2
1. A palindrome is a word which is the same backwards as forwards. Write a rule palindrome(List) which succeeds if List is the
same backwards as forwards. Test your rule.

Remember that the built-in predicate name converts an atom into a list of ASCII codes of its characters. This information
should enable you to put palindrome into a short program to test a typed in word to see whether it is a palindrome started by a

## The rule to test a palindrome list is amazingly simple:

palindrome(List) :- reverse(List,List).

## where reverse is defined (in your notes) as:

reverse(L1,L2) :- rev(L1,[],L2).
rev([],L,L).
rev([X|L],L2,L3) :- rev(L,[X|L2],L3).

## The program to test a palindrome could be:

2. Write a rule convert which works through a list of integers, takes all those between 64 and 91, and adds 32 to them. In other
words, if the list of integers is a list of ASCII codes, it converts all the ASCII codes of upper case characters to the ASCII
codes of their corresponding lower case characters.

convert([],[]).
convert([L|Lt],[Q|Qt]) :- L>64, L<91, Q is L+32, convert(Lt,Qt).
convert([L|Lt],[L|Qt]) :- convert(Lt,Qt).

## 3. You can put this rule into your program pal

palindrome(NewList).

4. Write a rule stars to draw a specified number of stars or asterisks in a horizontal line on the screen.

stars(0).
stars(N) :- write('*'), M is N-1, stars(M).

5. Share prices for a company at the end of each month are saved as facts of the form:

share(jan,25).
share(feb,15).
share(mar,24).
share(apr,12).
share(may,30).

## http://www2.dcs.hull.ac.uk/NEAT/dnd/AI/prolog/tutorial2.html (1 of 2) [03/03/2010 09:34:38 a.m.]

Prolog Tutorial 2

Write a rule display which writes these out in two columns on the screen.

## display :- share(Month,Price),write(Month),write(' '),write(Price),nl,fail.

display.

6. Now modify this rule to call the rule stars and draw the share price as lines of stars on the screen instead of writing the
numbers. Somethig like this:

jan *************************
feb ***************
mar ************************
apr ************
may ******************************

## display :- share(Month,Price),write(Month),write(' '),stars(Price),nl,fail.

display.

stars(0).
stars(N) :- write('*'), M is N-1, stars(M).