PPAI/L3

From WikiZMSI

Spis treści

Part 3- Lists

List is a simple data structure and it is a sequence of any number if items. List is a structure with a special name: ".".


  1. List notation:
 [ala]
 [ala,narty,ksiazki]
 [head,tail]
 [a,V,1,[c,s],p(X)]
 [1,2,3,4]

Task 1: Try each command in the query mode.

[1,2,3,4] = [1|[2,3,4]].
[1,2,3,4] = [1,2|[3,4]].
[1,2,3,4] = [1,2,3|[4]].
[1,2,3,4] = [1,2,3,4,[]].
[1,2,3,4] = [1|2,3,4].
[Head|Tail] = [1,2,3,4].
[Head|Tail] = [[1,1,ala],2,3,4].
[Head|Tail] = [X+Y,x+y].
p([_,_,_,[_|X]]) = p([alice, habe, very, [small, white, cat]]).
[a,[]] = [A,B|Rest].
[One] = [two|[]].

Basic operations on lists

Membership

member(X,L)

means X is an object and L is a List. The goal is true if X occurs in L, e.g. member(b,[a,b,c])

  • A program for membership could be written:
 my_member(X,[X|_]).
 my_member(X,[_|Tail]) :- my_member(X,Tail).
Task 2: Try each command in the question mode.
my_member(a,[b,c,[s,a],a]).
my_member(a,[b,c,[s,a]]).
my_member([s,a],[b,c,[s,a]]).
my_member(X,[a,b,c]).
my_member(a, X).
Build in clause for testing membership is member/2

Concatenation

conc(L1,L2,L3)

means L1 , L2, L3 are lists. The goal is true if L3 is the list containing elements of L1 and L2, e.g. conc([a,b],[1,2,3],[a,b,1,2,3])

  • A program for concatenation could be written:
 conc([],L,L).
 conc([X|L1],L2,[X|L3]) :- conc(L1,L2,L3).
Task 3: Try each command in the query mode.
conc([b,c,[s,a],a],[a],X).
conc([a],[b],[a,b]).
conc(L1,L2,[b,c,[s,a]]). 
conc(Before, [may|After], [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec])
Build in clause for testing membership is append/3

Adding

Is is simple the operation [X|L], where X is an element added to the list L.

Deleting

del(X,L1, L2)

means X is an object L1 , L2 are lists. The goal is true if L3 is the list containing elements of L2 except X, e.g. del(1,[1,2,3],[2,3])

  • A program for deleting the item could be written:
del(X,[X|L1],L1).
del(X,[Y|L2],[Y|L3]) :- del(X,L2,L3).
Task 4: Try each command in the query mode.
del(a,[s,a],X).
del(a,[a,b,a,a],X).
del(a,X,[b,c,d]).
del(X,[a,b,d,c],[a,d,c]).
Build in clause for testing membership is select/3 and delete/3. Try and find the difference

Other build in operators

Ask about build in clauses for lists:

apropos(list).

Tasks on lists

1. Define the caluse which prints all months prior and after the given month. Use the build in relation append/3. For example the true is obtained:

months(march,[january, february],[april, may, june, july, august, september, october, november, december])

Solution

2. Define a caluse which prints one month prior and one month after the given month. Use the build in relation append/3. For example the true is obtained:

months2(march, fabruary, april).

Solution

3. For a given list print out only three elements from the beginning. Use the build in relation append/3. For example the true is obtained:

list3b( [a,b,c], [a,b,c,w,e,r])

Solution

4. For a given list print out only three elements from the end. Use the build in relation append/3. For example the true is obtained:

list3e( [w,e,r], [a,b,c,w,e,r])

Solution

5. Define a relation that checks if the object X is the element of the list L using a build-in relation append/3. For example the true is obtained:

my_member2( w, [a,b,c,w,e,r])

Solution

6. Define relations even/1 and odd/1 that checks if the list L contains even or odd number of elements. For example the true is obtained:

even( [a,b,c,w,e,r]).
odd( [1]).
even([]).

Solution

7. Define a relation my_reverse/2 that reverse the given list L to a list L1. For example the true is obtained:

my_reverse( [a,b,c,w,e,r], [r,e,w,c,b,a]).

Solution

Solution1

8. Define a relation shift/2 that move the first element of the given list L to a last position and resulted list is L2. For example the true is obtained:

shift( [a,b,c,w,e,r], [b,c,w,e,r,a]).

Solution

9. Define a relation translate/2 that translate every number of the given list L into the word in the list L2. For example the true is obtained:

translate( [2,4,1], [two, four, one]).

Solution

10. Define a relation max_list/2 that find the maximum number X in the given list L. For example the true is obtained:

max_list( [2,4,1], 4).

Solution

11. Define a relation sum_list/2 that find the total sum X of all numbers in the given list L. For example the true is obtained:

sum_list( [2,4,1], 7).

Solution

12. Define a relation incresing/1 that checks if elements of L are in increasing order. For example the true is obtained:

increasing( [1,2,4,25]).

Solution

13. Define a relation find_last/2 that checks if an element X is the last element of L. For example the true is obtained:

find_last( 25, [1,2,4,25]).

14. Define a relation find_lastbutone/2 that checks if an element X is the last but one element of L. For example the true is obtained:

find_lastbutone(4, [1,2,4,25]).

15. Define a relation kthelement/3 that checks if an element X is the K-th element of list L. For example the true is obtained:

kthelement(b,2, [a,b,c,d]).