Sei sulla pagina 1di 7

‫جامعة الملك خالد‬

‫كلية علوم الحاسب اآللي‬


‫قسم علوم الحاسب اآللي‬

King Khalid University


College of Computer Science
Department of Computer Sciences

STUDENT LAB/TUTORIAL ACTIVITY REPORT

Course Code : CSM 375


Course Name : Artificial Intelligence
Section Number: 3102
Student ID :
Student Name :
EXPERIMENT/EXERCISE NO. : 04

TITLE OF EXPERIMENT/EXERCISE: To Demonstrate List Operations.

Goal AIM/OBJECTIVE (similar to the manual):

Description

Lists
So far we have only considered simple items as arguments to our programs.
However in Prolog a very common data-structure is the list.
Lists themselves have the following syntax. They always start and end with
square brackets, and each of the items they contain is separated by a comma.
Here is a simple list [a, freddie, A_Variable,apple]

Prolog also has a special facility to split the first part of the list (called the head)
away from the rest of the list (known as the tail). We can place a special symbol |
(pronounced 'bar') in the list to distinguish between the first item in the list and
the remaining list. For example, consider the following.

[first,second,third] = [A|B]
where A = first and B=[second,third]
The unification here succeeds. A is bound to the first item in the list, and B to the
remaining list.

Objectives: To Learn and apply list operators and write our own programs.

CASE STUDY/ RELEVANT PROGRAMS / DESIGN/CONFIGURATION /


IMPLEMENTATION/COMMANDS. (A PART FROM THE TEACHER TAUGHT IN THE

LAB)

Theory / Explanation:

Procedure:
Below shows some of the List operators
List Examples
Here are some example simple lists
[a,b,c,d,e,f,g]
[apple,pear,bananas,breadfruit]
[ ] /* this is a special list, it is called the empty list because it
contains nothing */
Now lets consider some comparisons of lists:
[a,b,c] unifies with [Head|Tail] resulting in Head=a and Tail=[b,c]
[a] unifies with [H|T] resulting in H=a and T=[]
[a,b,c] unifies with [a|T] resulting in T=[b,c]
[a,b,c] doesn't unify with [b|T]
[] doesn't unify with [H|T]
[] unifies with []. Two empty lists always match

Activity for Students: (Practical Exercise 4)

Consider the following fact.


p([H|T], H, T).
Let’s see what happens when we ask some simple queries.

?- p([a,b,c], X, Y).
Your Answer

?- p([a], X, Y).

Your Answer

?- p([], X, Y).
Your Answer

?- [X,Y,Z]=[jon,likes,mary].

Your Answer

?-[[the,Y]|Z]=[[X,mine],[is,there]].

Your Answer
Facts about: Length of a list.
size ([],0).
size([H|T],N) :- size(T,N1), N is N1+1.
?- size ([1, 2, 3, 4, 5], N).
Your answer N _______

?- size([ab1,cda2,er3],N).
Your answer N = _____.
?- size([ab1,[cda2,er3]],N).
Your answer N = _____.
?- size([a, [b, c, d], e, [f | g], h], N).
Your answer N= ______.
Fact:
append([],List,List).
append([Head|Tail],List2,[Head|Result]):-
append(Tail,List2,Result).
Query
?- append ([a,b,c],[one,two,three],Result).
Result:

Consider the following definition of the predicate 'member/2'.

member(X,[X|R]).
member(X,[Y|R]) :- member(X,R).
One can read the clauses the following way, respectively:

X is a member of a list whose first element is X.


X is a member of a list whose tail is R if X is a member of R.

?- member(2,[1,2,3]).
Your answer

?- member(X,[1,2,3]).
Your answer

Prolog code to find sum of n numbers


sumlist([],0).
sumlist([H|T],N):-sumlist(T,N1), N is N1+H.

sumlist([1,2,3),N).
Your answer:
ANALYSIS and OBSERVATION

CONCLUSION:

Reserved for the lab teacher

Observation
Awarded Mark:
SIGNATURE :
DATE:

Potrebbero piacerti anche