Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in.

Similar presentations


Presentation on theme: "1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in."— Presentation transcript:

1 1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in primitives

2 2 Unifications (cont. from last week) Will the following unifications succeed or fail? If succeed, what are the resulting instantiation of variables? f(g(A), g(a)) = f(g(n(1,2,3)), g(B)) p(A, B) = p(X, Y) g(A, B) = g(X, Y, Z) q(A, B) = q(X, X) 1+2*3+4 = X*Y 1+2*3+4 = X+Y

3 3 Lists A simple and most widely used structure. e.g. [tennis, skiing, travelling, music] [ ] an empty list A non-empty list has a head and tail. Head – the first element of the list Tail – the remaining part of the list e.g. the head of the above list is tennis, and the tail is [skiing, travelling, music]

4 4 Lists [H|T] represents a list with a head H and a tail T. The elements of a list can be any type. [1,f(2,3),g(x)] [[1,2],[a,b,c],[_,_,_,_]] Closed list – the length is fixed Open list – the last tail is a variable [a, b, c|X] Q: Is [a,b,c,X] as [a,b,c|X] ?

5 5 Recursion Recursive data structures – a structure contains its own type as substructures. List – has two parts, head and tail which is another list (same type). Recursion in program – (inductive definitions) When write a program, assume the program is already defined, and call it (with decomposed data) in the program

6 6 Manipulating Lists Ex. 1 - membership Write a program member(X,L) which is true if X occurs in list L. The program is based on the following observation: X is a member of L if either X is the head of L or X is a member of the tail of L. member(X,[X|_]). member(X,[_|T]):- member(X,T).

7 7 Manipulating Lists Ex. 2 – Processing element 1-by-1 Write a program check_type(L1,L2) which goes through all elements in list L1, checks their data type, and generates a corresponding type-list L2. check_type([],[]). check_type([X|T1],[var|T2]):- var(X), check_type(T1,T2). check_type([X|T1],[const|T2]):- atomic(X), check_type(T1,T2). ……

8 8 Manipulating Lists Ex. 3 – Processing element 2-by-2 Write a program swap(L1,L2) which swaps elements with their next neighbour. e.g. swap([a,b,c,d,f], L) returns L = [b,a,d,c,f] swap([],[]). swap([X],[X]). swap([X,Y|T1],[Y,X|T2]):- swap(T1,T2).

9 9 Manipulating Lists Ex. 4 – Processing element 2-by-1 Write a program sum2(L1,L2) which adds each pair of elements in L1 and leaves sums in L2. e.g. sun2([2,5,9,4,1], L) returns L = [7,14,13,5] sum2([],[]). sum2([X],[]). sum2([X,Y|T1],[Z|T2]):- Z is X+Y, sum2([Y|T1],T2).

10 10 Manipulating Lists Ex. 5 – Appending 2 lists (harder) Write a program append(L1,L2,L3) where L3 is the concatenation of L1 and L2. e.g. append([1,2], [a,b,c],L) returns L = [1,2,a,b,c] Q: how about append(L1,L2,[1,2,3]) ? append([],L,L). append([X|T1],T2,[X|T3]):- append(T1,T2,T3).

11 11 Manipulating Lists Ex. 6 – generating an integer list Write a program gen(L,X,Y) which generates an integer list between X and Y. e.g. ?- gen(L,0,10) returns L = [0,1,2,3,4,5,6,7,8,9,10] gen([X],X,X). gen([X|T],X,Y):- X < Y, NewX is X+1, gen(T,NewX,Y).

12 12 Some Prolog Built-in primitives To evaluate an arithmetic expression, Prolog provides a special built-in: X is Y where Y must be an arithmetic expression and X must be a number or a variable. It evaluates Y first, then unifies the result with X. Other built-ins which also force evaluation of an arithmetic expression are: X>Y X =Y X=<Y X=:=Y Built-ins for data type checking: var(X) number(X) integer(X) float(X) nonvar(X) atom(X) atomic(X)


Download ppt "1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in."

Similar presentations


Ads by Google