Lab-4 List in Prolog Term I 2013 Statement Outcome Discussions about list and operations on list in prolog. Activity Outcomes Students will understand about lists, how to use list operations on lists (add an element into the list, delete an element from the list, counting the number of elements in the list, modifying list, detecting an element in the list, appending one list to another, tracing the append goal with suitable examples.. Instructor Note Solve the exercise at the end of the notes. Notes List List is a sequence of any number of items Example : [mech, elec, civil, aero, cse, john, [1,2], [], [X]] List consists of two parts i.e L = [Head|Tail] : Head – First item of the list Tail – Remaining part of the list (tail itself is a list) Empty list is also a list ( [ ] ) Lists are handled in Prolog as trees List1=[a, b, c] [a, b, c] = [a | [b,c] ] = [a, b | [c] ] = [a, b, c| [ ] ] Membership member(X,L) : X is an object that occurs in L X is a member of list L if either: X is the head of L. or X is a member of the tail of L. member( X, [X| Tail] ). member( X, [Head| Tail] ) :member( X, Tail ). Example: ?- member( a, [a, b, c]). CPCS-331 - The Lab Note Lab-4 Lab-4 List in Prolog Term I 2013 Yes. ?- member( [b, c], [a, [b, c]]). Yes. ?- member(b, [a, [b, c]]). No. Concatenation conc(L1,L2,L3) : L3 is the concatenation of L1 & L2 If the first argument is empty list then second and third arguments must be the same list. conc([ ], L, L). If the first argument is non-empty list then it can be represented as [X|L1]. Then the result of concatenation will be [X|L3] where L3 is the concatenation of L1 and L2. conc([X|L1], L2, [X|L3]) :- conc(L1, L2, L3). Examples: ?- conc( [a,[b,c],d], [a, [ ],b], L). L = [a, [b,c], d, a, [ ], b] ?- conc( Before, [feb| After], [ jan, feb, mar]). Before = [ jan ] After = [ mar ] Note: Refer to rule conc([X|L1], L2, [X|L3]) :- conc(L1, L2, L3). Deleting an item del(X,L,L1) : L1 is equal to L with the first occurrence of item X removed. If X is the head of the list then the result after deletion is the tail of the list. del( X, [X| Tail], Tail). If X is in the tail then it is deleted from there. del(X, [Y| Tail], [Y| Tail1]) :- del(X, Tail, Tail1) Example: CPCS-331 - The Lab Note Lab-4 Lab-4 List in Prolog Term I 2013 ?- del(a, [a, b, a, c], L). L = [b, a, c]; L = [a, b, c]; No Inserting an item insert(X,L,L1) : L1 is any list such that deleting X from L1 gives L. insert( X, List, BiggerList) :del( X, BiggerList, List). Example: ?- insert( a, [ b, c, d], L). L = [a, b, c, d]; L = [b, a, c, d]; L = [b, c, a, d]; L = [b, c, d, a]; No Adding an item add(X,L,L1) : L1 is the list obtained by adding X to L. X is added in front of the list L. The resulting list L1 is simply [X|L]. add(X,L,[X|L]). Example: ? add(4.2, [1, a, f, [2] ], L1). L1 = [4.2, 1, a, f, [2] ] Sub list S is a sublist of L if: L can be decomposed into two lists, L1 and L2, & L2 can be decomposed into two lists, S and some L3 CPCS-331 - The Lab Note Lab-4 Lab-4 List in Prolog Term I 2013 Sublist sublist(S,L):conc(L1,L2,L), conc(S,L3,L2) Example: ? sublist([b,c],[a,b,c,d,e]). Yes ? sublist (S, [a,b,c]). S = [ ]; S = [a]; S = [a,b]; S = [a,b,c]; S = [ ]; S= [b]; … Operators Some of the operators are in-built in Prolog. Some predefined arithmetic operators: + addition - subtraction * multiplication / division mod modulo, the remainder of integer division CPCS-331 - The Lab Note Lab-4 Lab-4 List in Prolog Term I 2013 Precedence of operator decides the correct interpretation of expressions. Operator with highest precedence is the principle functor of the term. Operators are functors. 3+4 = 3+4 ( 3+4 ≠ 7) New operators can be defined by inserting into special kind of clauses called directives. Example: :-op(600,xfx,<==>) Three group of operators: infix: xfx, xfy, yfx prefix: fx, fy postfix: xf, yf These definition helps in unambiguous interpretation of expressions which have sequence of binary operators. x represents an argument whose precedence must be strictly lower than that of operator y represents an argument whose precedence is lower or equal to that of operator Precedence of an argument in parentheses or unstructured object is zero Precedence of a structured argument is equal to the precedence of its principal functor Example: :-op(500,yfx,- ) a–b–c Interpreted as (a-b)-c and not as a-(b-c) Arithmetic ‘=‘ is matching operator (it does not evaluate on its own) Matches right hand side with left hand side Examples: 1. ? X=1+2 X=1+2 2. ? 1+3=2+2 No 3. ?X+3=4+Y CPCS-331 - The Lab Note Lab-4 Lab-4 List in Prolog Term I 2013 X=4 Y=3 ‘is’ operator forces evaluation of expression on RHS forcing instantiation of values on LHS to the evaluated value Examples: 1. X is 1+2 X=3 1. Y is 5 mod 2 Y=1 At the time of evaluation all arguments must be instantiated to numbers Values of arithmetic expressions can be compared by arithmetic operators: X<Y :- X is less than Y X>Y :- X is greater than Y X >=Y :- X is greater than or equal to Y X =<Y :- X is less than or equal to Y X =:= Y :- the values of X and Y are equal X =\= Y :- the values of X and Y are not equal Calculating number of elements in a list length(List,N) % N is the number of elements in List length([ ], 0). %R1 length( [Head|Tail], N):length(Tail, N1), N is 1+N1. %R2 Query: ? length ([a,b,c,d],N). N=4 CPCS-331 - The Lab Note Lab-4 Lab-4 List in Prolog Term I 2013 • List processing allows handling objects that contain an arbitrary number of elements. • A list is an object that contains an arbitrary number of other objects. • A list that contains the numbers 1,2 and 3 is written as [1,2,3] . • Each item contained in a list is known as an element. • To declare the domain of a list of integers: – Domains – intlist= integer * (means list of integers) • The elements of a list must be of a single domain. • To define a list of mixed types use compound objects as follows: • Domains • • – elementlist= element * – element=ie (integer); re ( real); se (string) A list consists of two parts: – The head : the first element of the list – The tail : a list of the remaining elements. Example: if l=[a,b,c] the head is a and the tail is [b, c]. Procedure CPCS-331 - The Lab Note Lab-4 Lab-4 List in Prolog Term I 2013 • If you remove the first element from the tail of the list enough times, you get down to the empty list ([ ]). • The empty list can not be broken into head and tail. • A list has a tree structure like compound objects. • Example: the tree structure of [a, b,c, d] is drawn as shown list in the figure. a Note : the one element list [a] is not as the element a since [a] is really the compound data structure shown here [a] a [] b list list c list d [] List processing • Prolog allows you to treat the head and tail explicitly. You can separate the list head and tail using the vertical bar (|). • Example : • [a, b, c ] ≡[a | [b, c ] ] ≡[a | [b| [c ] ] ] ≡[a | [b| [c| [] ] ] ] ≡[a , b| [c ] ] List unification: the following table gives examples of list unification: CPCS-331 - The Lab Note Lab-4 Lab-4 List in Prolog Term I 2013 List unification: the following table gives examples of list unification: list1 list2 Variable binding [X, Y, Z] [ book, ball, pen ] X=book, Y=ball, Z=pen [7] [X|Y] X=7,Y=[ ] [1, 2, 3, 4] [X, Y|Z ] X=1,Y=2,Z=[3,4] [1,2] [3|X] fail Using lists – Since a list is really a recursive compound data structure, you need recursive algorithms to process it. – Such an algorithm is usually composed of two clauses:- DOMAINS list= integer * PREDICATES write_a_list( list ) Do nothing but report success. CLAUSES • One to deal with empty list. write_a_list([ ]). • The other deals with ordinary list (having head and tail). T=[ ] write_a_list ( [ H | T ] ):- • Example: the shown program writes the elements of an integer list. write(H), nl, write_a_list( T). Write the head Write the Tail list GOAL write_a_list([ 1, 2, 3]). CPCS-331 - The Lab Note Lab-4 Lab-4 List in Prolog Term I 2013 Counting list elements • • DOMAINS Two logical rules are used to determine the length of a list 1. The length of [ ] is 0. 2. The length of the list [X|T] is 1+the length of T. The shown Prolog program counts the number of elements of a list of integers (can be used for any type). list= integer * PREDICATES length_of ( list, integer) CLAUSES length_of ( [], 0). T=[ ] Length_of ( [ H | T ],L ):length_of ( T ,M ), L=M+1. L=3 GOAL length_of([ 1, 2, 3],L). Modifying the list Modifying the list • The following program adds 1 to each element of a list L1 by making another list L2 – If L1 is [ ] then L2=[ ] – If L1=[H1|T1] assuming L2=[H2|T2] then • H2=H1+1. • Add 1 to T1 by the same way Removing Elements from a list CPCS-331 - The Lab Note Lab-4 Term I 2013 Lab-4 List in Prolog Removing Elements from a list • The following program removes negative elements from a list L1 by making another list L2 that contains non negative numbers of L1 : – If L1 is [ ] then L2=[ ] – If L1=[H1|T1] and H1<0 then neglect H1 and process T1 – Else make head of L2 H2=H1 and Repeat for T1. List Membership • To detect that an element E is in a list L: – If L=[H|T] and E=H then report success Else – search for E in T CPCS-331 - The Lab Note Lab-4 Lab-4 List in Prolog Term I 2013 Appending one list to another • To append L1 to L2 we get the result in L3 as follows • Append (L1,L2,L3): – If L1=[ ] then L3=L2. – Else – Make H3=H1 and – make T3 =T1 +L2 Exercise: Write a program that adds 1 to each element of the list, remove element from the list, search for an element in the list, and append one list to another.(4 programs). CPCS-331 - The Lab Note Lab-4
© Copyright 2026 Paperzz