0% found this document useful (0 votes)
180 views34 pages

Prolog Lists in AI Programming

This document discusses programming languages in artificial intelligence, specifically Prolog. It covers lists as a data structure in Prolog, including representation of lists and common operations on lists such as membership, length calculation, concatenation, deletion from a list, appending to a list, insertion into a list, and repositioning list items. It also discusses handling input and output in Prolog using predicates like write(), read(), and tab() as well as reading from and writing to files.

Uploaded by

Aakansha Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views34 pages

Prolog Lists in AI Programming

This document discusses programming languages in artificial intelligence, specifically Prolog. It covers lists as a data structure in Prolog, including representation of lists and common operations on lists such as membership, length calculation, concatenation, deletion from a list, appending to a list, insertion into a list, and repositioning list items. It also discusses handling input and output in Prolog using predicates like write(), read(), and tab() as well as reading from and writing to files.

Uploaded by

Aakansha Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Artificial Intelligence

(5A24ARI)

Chapter-2: Programming Languages in AI-


Prolog
Presented By
Prof. Aakansha Saxena
Assistant Professor
Rashtriya Raksha University
Lists
•  It is a data structure that can be used in different cases for non-
numeric programming. Lists are used to store the atoms as a
collection.
• List consists of any number of items, for example, red, green, blue,
white, dark. It will be represented as, [red, green, blue, white, dark].
The list of elements will be enclosed with square brackets
Representation of Lists

• A list can be either empty or non-empty. In the first case, the list is


simply written as a Prolog atom, []. In the second case, the list consists of
two things as given below −
• The first item, called the head of the list;
• The remaining part of the list, called the tail.
• Suppose we have a list like: [red, green, blue, white, dark]. Here the head
is red and tail is [green, blue, white, dark]. So the tail is another list.
• Now, let us consider we have a list, L = [a, b, c]. If we write Tail = [b, c]
then we can also write the list L as L = [ a | Tail]. Here the vertical bar (|)
separates the head and tail parts.
• So the following list representations are also valid −
• [a, b, c] = [x | [b, c] ]
• [a, b, c] = [a, b | [c] ]
• [a, b, c] = [a, b, c | [ ] ]
• For these properties we can define the list as −
• A data structure that is either empty or consists of two parts − a head
and a tail. The tail itself has to be a list.
Operations on Lists
Membership Operation

• During this operation, we can check whether a member X is present in


list L or not? So how to check this? Well, we have to define one
predicate to do so. Suppose the predicate name is list_member(X,L).
The goal of this predicate is to check whether X is present in L or not.
• To design this predicate, we can follow these observations. X is a
member of L if either −
• X is head of L, or
• X is a member of the tail of L
Length Calculation

• Used to find the length of list L. We will define one predicate to do


this task. Suppose the predicate name is list_length(L,N). This takes L
and N as input argument. This will count the elements in a list L and
instantiate N to their number. As was the case with our previous
relations involving lists, it is useful to consider two cases −
• If list is empty, then length is 0.
• If the list is not empty, then L = [Head|Tail], then its length is 1 +
length of Tail.
Concatenation

• Concatenation of two lists means adding the list items of the second
list after the first one. So if two lists are [a,b,c] and [1,2], then the final
list will be [a,b,c,1,2]. So to do this task we will create one predicate
called list_concat(), that will take first list L1, second list L2, and the L3
as resultant list. There are two observations here.
• If the first list is empty, and second list is L, then the resultant list will
be L.
• If the first list is not empty, then write this as [Head|Tail], concatenate
Tail with L2 recursively, and store into new list in the form, [Head|
New List].
Output
| ?- [list_basics].
compiling D:/TP Prolog/Sample_Codes/list_basics.pl for byte code...
D:/TP Prolog/Sample_Codes/list_basics.pl compiled, 7 lines read - 1367 bytes written, 19 ms

yes
| ?- list_concat([1,2],[a,b,c],NewList).

NewList = [1,2,a,b,c]

yes
| ?- list_concat([],[a,b,c],NewList).

NewList = [a,b,c]

yes
| ?- list_concat([[1,2,3],[p,q,r]],[a,b,c],NewList).

NewList = [[1,2,3],[p,q,r],a,b,c]

yes
| ?-
Delete from List

• Suppose we have a list L and an element X, we have to delete X from


L. So there are three cases −
• If X is the only element, then after deleting it, it will return empty list.
• If X is head of L, the resultant list will be the Tail part.
• If X is present in the Tail part, then delete from there recursively.
Append into List

• Appending two lists means adding two lists together, or adding one
list as an item. Now if the item is present in the list, then the append
function will not work. So we will create one predicate namely,
list_append(L1, L2, L3). The following are some observations −
• Let A is an element, L1 is a list, the output will be L1 also, when L1 has
A already.
• Otherwise new list will be L2 = [A|L1].
Insert into List

• This method is used to insert an item X into list L, and the resultant
list will be R. So the predicate will be in this form list_insert(X, L, R). So
this can insert X into L in all possible positions. If we see closer, then
there are some observations.
• If we perform list_insert(X,L,R), we can use list_delete(X,R,L), so
delete X from R and make new list L.
Repositioning operations of list items
Permutation Operation

• This operation will change the list item positions and generate all
possible outcomes. So we will create one predicate as list_perm(L1,L2),
This will generate all permutation of L1, and store them into L2. To do
this we need list_delete() clause to help.
• To design this predicate, we can follow few observations as given below −
• X is member of L if either −
• If the first list is empty, then the second list must also be empty.
• If the first list is not empty then it has the form [X | L], and a permutation
of such a list can be constructed as, first permute L obtaining L1 and then
insert X at any position into L1.
Reverse Operation

• Suppose we have a list L = [a,b,c,d,e], and we want to reverse the


elements, so the output will be [e,d,c,b,a]. To do this, we will create a
clause, list_reverse(List, ReversedList). Following are some
observations −
• If the list is empty, then the resultant list will also be empty.
• Otherwise put the list items namely, [Head|Tail], and reverse the Tail
items recursively, and concatenate with the Head.
• Otherwise put the list items namely, [Head|Tail], and reverse the Tail
items recursively, and concatenate with the Head.
Shift Operation

• Using Shift operation, we can shift one element of a list to the left
rotationally. So if the list items are [a,b,c,d], then after shifting, it will
be [b,c,d,a]. So we will make a clause list_shift(L1, L2).
• We will express the list as [Head|Tail], then recursively concatenate
Head after the Tail, so as a result we can feel that the elements are
shifted.
• This can also be used to check whether the two lists are shifted at one
position or not.
Order Operation

• Here we will define a predicate list_order(L) which checks whether L is


ordered or not. So if L = [1,2,3,4,5,6], then the result will be true.
• If there is only one element, that is already ordered.
• Otherwise take first two elements X and Y as Head, and rest as Tail. If
X =< Y, then call the clause again with the parameter [Y|Tail], so this
will recursively check from the next element.
Handling input and output

The write() Predicate


• To write the output we can use the write() predicate. This predicate
takes the parameter as input, and writes the content into the console
by default. write() can also write in files. Let us see some examples of
write() function
The read() Predicate

• The read() predicate is used to read from console. User can write
something in the console, that can be taken as input and process it.
The read() is generally used to read from console, but this can also be
used to read from files. Now let us see one example to see how read()
works.
tab() Predicate

• The tab() is one additional predicate that can be used to put some
blank-spaces while we write something. So it takes a number as an
argument, and prints those many number of blank spaces.
Reading/Writing Files

• How can use files to read from, and write into the files. There are
some built-in predicates, that can be used to read from file and write
into it.
• The tell and told
• If we want to write into a file, except the console, we can write
the tell() predicate. This tell()predicate takes filename as argument. If
that file is not present, then create a new file, and write into it. That
file will be opened until we write the told command. We can open
more than one file using tell(). When told is called, all files will be
closed.

You might also like