0% found this document useful (0 votes)
6 views

Hiren Ai 3

Uploaded by

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

Hiren Ai 3

Uploaded by

Patel Hiren
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No.

3
Name: Hiren Daxeshbhai Patel Roll No.: 07

Objective: Implementation of List in SWI Prolog.

Resources used:

• Windows 10,11
• PC i5 or greater
• Software SWI Prolog

Theory:

• List:
• Lists are an ordered collection of data or items, which can be different types. Lists are
adaptable – they can contain duplicate members, be reordered, added to and edited; as
such, they are described as being mutable.
• A list in Prolog is an ordered collection of items denoted as [i1, i2, …, in].
• Unlike arrays in other programming languages where we can directly access any
element of the array, prolog lists allow direct access of the first element only which is
denoted as Head. Therefore we can write a prolog list as : [Head| Rest], where Rest is
the rest of the list excluding the first element Head.
• Prolog lists allow nonhomogeneous data types of list items.
• Nested lists of arbitrary depths are also allowed in prolog.

• Syntax:
[a | L]
Here L is a list and a is a single element.

• Operators :
• Pipe (|) operator: In prolog, lists have got only one operator, called pipe, denoted by
|. This operator is used to append an element at the beginning of a list.
• Grade• Cut(!) operator: In Prolog, the Cut operator, denoted by ! is a goal that
always succeeds but cannot be backtracked. For example, consider the following
program to find the max element between 2 numbers.
• Operations on list in prolog:
• Find the length of a list:

list_length([], 0).
list_length([_ | L], N) :-
list_length(L, N1),
N is N1 + 1.
% If length of L is N1, then length of [_ | L] will be N1 + 1

• Determine whether an element x belongs to a list.

is_member(X, [X | _]) :- !. % If the head of the list is X

is_member(X, [_ | Rest]) :- % else recur for the rest of the list


is_member(X, Rest).

• Append a list L2 at the end of another list L1 and put the resultant list in L3.

% If L1 is empty, resultant list will be equal to L2 (base case) append_list([],


L2, L2).

append_list([X | L1], L2, [X | L3]) :-


append_list(L1, L2, L3).

• Insert an element X at the end of a list.


insert_end(L, X, NewL) :-
append_list(L, [X], NewL).

• Delete the first occurrence of an element from a list.

% Deletion of any element from empty list will produce empty list(base case)
delete_element(_, [], []).

delete_element(X, [X | L], L) :- !.

delete_element(X, [Y | L], [Y | L1]) :- delete_element(X,


L, L1).
Source Code:

list_member(X,[X|_]).

list_member(X,[_|TAIL]):-list_member(X,TAIL).

list_length([],0). list_length([_|TAIL],N):-list_length(TAIL,N1),N

is N1+1.

list_concat([],L,L).

list_concat([X1|L1],L2,[X1|L3]):- list_concat(L1,L2,L3).

list_delete(X, [X], []). list_delete(X,[X|L1], L1).

list_delete(X, [Y|L2], [Y|L1]) :- list_delete(X,L2,L1).

list_member(X,[X|_]). list_member(X,[_|TAIL]) :-

list_member(X,TAIL). list_append(A,T,T) :-

list_member(A,T),!. list_append(A,T,[A|T]).

list_delete(X, [X], []). list_delete(X,[X|L1], L1).

list_delete(X, [Y|L2], [Y|L1]) :- list_delete(X,L2,L1).

list_insert(X,L,R) :- list_delete(X,R,L).

list_delete(X,[X|L1], L1). list_delete(X, [Y|L2],

[Y|L1]) :- list_delete(X,L2,L1).

list_perm([],[]).

list_perm(L,[X|P]) :- list_delete(X,L,L1),list_perm(L1,P). list_concat([],L,L).

list_concat([X1|L1],L2,[X1|L3]) :- list_concat(L1,L2,L3). list_rev([],[]).

list_rev([Head|Tail],Reversed) :- list_rev(Tail,

RevTail),list_concat(RevTail, [Head],Reversed).
list_concat([],L,L). list_concat([X1|L1],L2,[X1|L3]) :-

list_concat(L1,L2,L3).

list_shift([Head|Tail],Shifted) :- list_concat(Tail, [Head],Shifted).

list_order([X, Y | Tail]) :- X =< Y, list_order([Y|Tail]).

list_order([X]). list_subset([],[]).

list_subset([Head|Tail],[Head|Subset]) :- list_subset(Tail,Subset).

list_subset([Head|Tail],Subset) :- list_subset(Tail,Subset).

list_member(X,[X|_]). list_member(X,[_|TAIL]) :-

list_member(X,TAIL). list_union([X|Y],Z,W) :-

list_member(X,Z),list_union(Y,Z,W). list_union([X|Y],Z,[X|W]) :- \+

list_member(X,Z), list_union(Y,Z,W). list_union([],Z,Z).

list_member(X,[X|_]).

list_member(X,[_|TAIL]) :- list_member(X,TAIL).list_intersect([X|Y],Z,[X|W]) :-

list_member(X,Z), list_intersect(Y,Z,W).

list_intersect([X|Y],Z,W) :-

\+ list_member(X,Z), list_intersect(Y,Z,W). list_intersect([],Z,[]).

Output:
Conclusions:

Successfully Implementated List in SWI Prolog.

You might also like