Sie sind auf Seite 1von 12

Presentation of

Artificial
Intelligence

Submitted by:Shubham Agarwal(16211)


B.Tech(C.S.)
Section B

Structures and Trees

It is usually easier to understand the form of a


complicated structure if we write it as a tree in
which each functor is a node and the components
are branches.
Each branch may point to another structure so we
can have structures within structures.
Rules: Root at the top and branches at the bottom.

For instance,
parents(charles,elizabeth,philip) is
written as

Another instance,
book(moby_dick,author(herman,melvill
e)) is written as

Lists
List is an ordered sequence of elements that can have
any length.
Here Ordered means order of the elements in the
sequence matters.
The elements of a list may be any term-constants,
variables, structures.
List can be represented as a special kind of tree.
A list is either an empty list ,having no elements or it is
a structure that has two components: head and tail.
Empty list is written as [].
The head and tail of a list are components of the
functor named .,which is a dot(called the period or
full stop).

For instance, the list containing of one element a is


.(a,[]) and its tree look like

Another instance .(a,.(b,.(c,[])))

A notation for this consist of the elements of the list


separated by commas and the whole list is enclosed in
square brackets.
For example: the previous lists can be written as [a]
Lists are manipulated by splitting them up into a head
or tail.
When a list appears in the square bracket notation, the
head of the list is the first element of the list.
The tail of the list is a list that consists of every element
except the first.

Because a common operation with lists is to split a list


into its head and tail ,there is a special notation in Prolog
to represent the list with head X and tail Y .
It is written as [X|Y].
A pattern of the form [X|Y] will instantiate X to the head of
a list and Y to tail of the list ,as in the following example.

Recursive Search
We frequently need to search inside a Prolog structure
to find some desired piece of information. When the
structure may have other structures as its
components ,this results in a recursive search task.
Suppose for example we have list of the names of
those horses sired by Coriander who all won horse
races in Great Britain in the year 1927

Now suppose we want to find out if a given horse is in the


list. The way we do this in Prolog is to find out whether
the horse is the same as the head of the list ;if it is we
succeed.
If it is not then we check to see if the horse is in the tail of
the list. This means checking the head of the tail next
time. And the head of that tail after that. If we come to
end of the list which will be the empty list, we must fail:
the horse is not in the list.
A relationship between an object and a list is called
membership.
The goal member(X,Y) is true if the term that X stands
for is a member of the list that Y stands for.
There are two conditions to check
I. It is a fact that X will be a member of Y if X is same
as the head of Y.
member(X,[X|]).

The second rule says that X is a member of a list providing


it is in the tail ,say Y, of that list.
member(X,[_|Y]):- member(X,Y).
Which represents X is a member of the list if X is a member
of the tail of the list.
The two boundary conditions for the member predicate :
1. The first boundary condition of member is recognised by
the first clause, which will cause the search through the
list to be stopped if the first argument of member
matches the head of the second argument.
2. The second boundary condition occurs when the
argument of member is empty list.
Each time member attempts to satisfy itself, the goal is
given a shorter list. Eventually one of two things will
happen; either the first member rule will match or member
will be given a list of length 0,the empty list as its second
argument. When either of these things happens ,the
recurrence of member goals will come to an end.

Das könnte Ihnen auch gefallen