معمل ذكاء-5

advertisement

يعانطصلاا ءاكذلا

Artificial Intelligence ( AI) comp323

Lab(5)

Lists

• it is a finite sequence of elements.

examples of lists in Prolog:

• [mia, vincent, jules, yolanda]

• [mia, robber(honey_bunny), X, 2, mia]

• []

• [mia, [vincent, jules], [butch, girlfriend(butch)]]

• [[], dead(zed), [2, [b, chopper]], [], Z, [2, [b, chopper]]]

We can learn some important things from these examples.

• We can specify lists in Prolog by enclosing the elements of the list in square brackets (that is, the symbols [ and ]).

• The elements are separated by commas.

• The length of a list is the number of elements it has.

• all sorts of Prolog objects can be elements of a list.

• there is a very special list, the empty list. The empty list is the list that contains no elements.

What is the length of the empty list? Zero.

• lists can contain other lists as elements .

• What is the length of the fourth list?

• Any non-empty list can be consisting of two parts: the head and the tail.

• The head is the first item in the list;

• the tail is everything else (the tail is the list that remains when we take the first element away)

• the tail of a list is always a list again

• Prolog has a special inbuilt operator | which can be used to decompose a list into its head and tail

.

• ?- [Head| Tail] = [mia, vincent, jules, yolanda].

• ?- [X|Y] = [mia, vincent, jules, yolanda].

• ?- [X|Y] = [].

But we can do a lot more with |

• ?- [X,Y | W] = [[], dead(zed), [2, [b, chopper]], [], Z].

• So, | can not only be used to split a list into its head and its tail, but we can in fact use it to split a list at any point .

• ?- [_,X,_,Y|_] = [[], dead(zed), [2, [b, chopper]], [], Z].

• ?- [_,_,[_|X]|_] = [[], dead(zed), [2, [b, chopper]], [], Z, [2, [b, chopper]]].

Member

• member(X,[X|T]).

• member(X,[H|T]) :- member(X,T).

• ?- member(yolanda,[yolanda,trudy,vincent,jules]).

• ?- member(vincent,[yolanda,trudy,vincent,jules]).

• ?- member(zed,[yolanda,trudy,vincent,jules]).

• It does this by stepwise breaking down the list into smaller lists, and looking at the first item of each smaller list.

• The empty list cannot be broken down into smaller parts, and this allows a way out of the recursion.

• ?- member(X,[yolanda,trudy,vincent,jules]).

• member(X,[X|_]).

• member(X,[_|T]) :- member(X,T).

Recursing down lists

• Member works by recursively working down a list, doing something to the head, and then recursively doing the same thing to the tail.

• When working with lists, we often want to compare one list with another, or to copy bits of one list into another, or to translate the contents of one list into another, or something similar.

• Let’s suppose we need a predicate a2b/2 that

• takes two lists as arguments,

• and succeeds if the first argument is a list of as,

• and the second argument is a list of bs of

exactly the same length.

• For example, if we pose the following query

• ?- a2b([a,a,a,a],[b,b,b,b]).

• ?- a2b([a,a,a,a],[b,b,b]).

• ?- a2b([a,c,a,a],[b,b,5,4]).

• what is the shortest possible list of as?

• And what is the shortest possible list of bs?

• So the most basic information our definition needs to contain is

• a2b([],[]).

• when the head of the first list is an a,

• and the head of the second list is a b,

• and a2b/2 decides that the two tails are lists of as and bs of exactly the same length!

• This gives us the following rule:

• a2b([a|Ta],[b|Tb]) :- a2b(Ta,Tb).

• ?- a2b([a,a,a,a],X).

• ?- a2b(X,[b,b,b,b]).

• ?- a2b(X,Y).

• Exercise 4.1

– 2,3,8

• Exercise 4.2

• Exercise 4.3

H.W

Download