artificial intelligence course (40417) sharif university of...

30
Artificial Intelligence Course (40417) Sharif University of Technology

Upload: doankhuong

Post on 18-Jul-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Artificial Intelligence Course (40417)Sharif University of Technology

Representation of lists Some operations on lists

Membership

Concatenation

Adding an item

Deleting an item

Sublist

Permutations

Representation of lists Some operations on lists

Membership

Concatenation

Adding an item

Deleting an item

Sublist

Permutations

The list is

a simple data structure widely used in non-numeric programming

a sequence of any number of items

[ ann, tennis, tom, skiing]

Empty list: a Prolog atom, []. Non empty list consisting of:

1. the first item, called the head of the list;

2. the remaining part, called the tail.

the head can be any Prolog object, the tail has to be a list.

? Hobbies1 = [tennis, music] ,Hobbies2 = [ skiing, food],L = [ ann, Hobbies1 , tom, Hobbies2].

Hobbies1 = [ tennis, music]Hobbies2 = [ skiing, food]L = [ ann, [tennis,music], tom, [skiing,food] ]

It is often practical to treat the whole tail as a single object. For example:

L = [a,b,c]

Tail = [b,c] and

L = [a I Tail]

vertical bar

Prolog accepts lists written as:

[ Item1 , Item2, ... ]

[ Head I Tail]

[Item1, Item2, ... | Others]

Representation of lists Some operations on lists

Membership

Concatenation

Adding an item

Deleting an item

Sublist

Permutations

Lists can be used to represent sets,note that: the order of elements in a set does not matter. same object can occur repeatedly in a list.

the most common operations on lists whether some object is an element of a list,

corresponds to the set membership; concatenation of two lists, correspond to the union of

sets; adding a new object to a list, or deleting some object

from it.

The goal member( X, L) is true if X occurs in L. where X is an object and L is a list

X is a member of L if either:

1. X is the head of L, or

2. X is a member of the tail of L.

member( X, [X I Tail] ) .member( X, [Head I Tail] ) :-member( X, Tail).

The goal conc( L1, L2, L3) is true if L1 and L2 are two lists, and L3 is their concatenation

if the first argument is the empty list then the second and the third arguments must be the same list; conc( [], L, L).

If the first argument of conc is a non-empty list then it has a head and a tail and must look like this: [X I L1]

conc( [], L, L).conc( [X I L1], L2, [X I L3]) :-conc(L1,L2,L3).

?- conc( [a,[b,c],d], [a,[],b], L).L = [a, [b,c], d, a, [], b]

we can use conc in the inverse direction for decomposing a given list into two lists:

?-conc( L1, L2, [a,b,c] ).

conc( Before,[may | After],

[jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec] ).

Before = [jan,feb,mar,apr]

After = [jun,jul,aug,sep,oct,nov,dec]

?- conc( _ , [Month1,may,Month2], [jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec] ).

Month l = apr

Month2 = jun

?- L1 = [a,b,z,z,c,z,z,z,d,e],

conc( L2, [z,z,z| _], L1).

L1 = [a,b,z,z,c,z,z,z,d,e]

L2 = [a,b,z,z,c]

member1( X, L) :- conc(L1, [X | L2], L).

Note the difference between member and member1!

member1( X, L):- conc( _, [X | _], L).

Easiest way is to put the new item in front of the list.

If X is the new item and the list to which X is added is L then the resulting list is simply: [X I L]

add( X, L, [X I L] ).

The goal del( X, L, L1) is true if L1 is equal to the list L with the item X removed.

1. If X is the head of the list then the result after the deletion is the tail of the list.

2. If X is in the tail then it is deleted from there.

del( X, [X I Tail], Tail).del( X, [Y I Tail], [Y I Tail 1] ) :-del( X, Tail, Tail1).

?- del( a, L, [1,2,3] ).

L = [a,1,2,3];

L = [1,a,2,3];

L = [1,2,a,3];

L = [1,2,3,a];

no

insert( X, List, BiggerList) :- del( X, BiggerList, List).

Some X is a member of List if X can be deleted from List:

member2( X, List):-del( X, List, _).

The goal sublist( L1, L2 ) is true if L1 is a sublist of L2.

S is a sublist of L if:

1. L can be decomposed into two lists, L1 and L2, and

2. L2 can be decomposed into two lists, S and some L3.

sublist( S, L) :-conc( L1, L2, L),conc( S, L3, L2).

The goal permutation( L, P) is true id P is a permutation of L.

The intention is to generate permutations of a list through backtracking using the permutation procedure.

1. If the first list is empty then the second list must also be empty.

2. If the first list is not empty then it has the form [X I L], and a permutation of such a list can be constructed in this way: first permute L obtaining L1 and then insert X at any position into Ll.

Permutation( [], [] ).

permutation( [X I L], P):-permutation( L, L1),insert( X, L1, P).

The difference?!.

permutation2( [], [] ).

permutation2( L, [X I P] ) :-del( X, L, L1),permutation2( L1, P)

Any question?!