Prolog Lists Advanced
Prolog Lists Advanced
2011
Prolog
1/22
References
[1] Ulf Nilsson, Jan Maluszy nski: Logic, Programming and Prolog, John Wiley & Sons Ltd., pdf, http://www.ida.liu.se/ ulfni/lpp [2] Dennis Merritt: Adventure in Prolog, Amzi, 2004 http://www.amzi.com/AdventureInProlog [3] Quick Prolog: http://www.dai.ed.ac.uk/groups/ssp/bookpages/quickprolog/quickprolog.html [4] W. F. Clocksin, C. S. Mellish: Prolog. Programowanie. Helion, 2003 [5] SWI-Prologs home: http://www.swi-prolog.org [6] Learn Prolog Now!: http://www.learnprolognow.org [7] http://home.agh.edu.pl/ ligeza/wiki/prolog [8] http://www.im.pwr.wroc.pl/ przemko/prolog
Prolog
2/22
check if the list is empty; an empty list is sorted, if the list is not empty, then:
1 2 3
take of the Head, sort the Tail, insert Head into an appropriate place of Tail.
Insert sort
1 2 3 4 5 6 7 8 9 10
Prolog
3/22
Sorting by Minimum/Maximum
Sorting by nding minimal/maximal element
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
min([X],X):-!. min([P|R],P):- min(R,X),X > P,!. min([P|R],X):- min(R,X),X =< P. max([X],X):-!. max([P|R],P):- max(R,X),X =< P,!. max([P|R],X):- max(R,X),X > P. min-sort([],[]):- !. min-sort(L,[M|LS]):min(L,M), select(M,L,LM), min-sort(LM,LS). min-sort-iter(L,LS):msi(L,[],LS). msi([],LS,LS):- !. msi(L,LA,LS):max(L,M), select(M,L,LM),!, msi(LM,[M|LA],LS).
Prolog
4/22
Bubblesort
Idea of bubblesort
1 2 3
scan by pairs of elements from left to right, if the order is wrong correct; continue the scan, if no correction takes place, the list is sorted.
Bubblesort
1 2 3 4 5 6
Bubblesort 2a
1 2 3 4
Prolog
5/22
Mergesort
Mergesort: split, sort, merge
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
lse([],W1,W2,W1,W2,_):- !. lse([H|T],L1,L2,W1,W2,l):- lse(T,[H|L1],L2,W1,W2,p), !. lse([H|T],L1,L2,W1,W2,p):- lse(T,L1,[H|L2],W1,W2,l), !. split([],[],[]). split([H|T],[H|U],V):- split(T,V,U). merge([H1|T1],[H2|T2],[H1|T]):H1 < H2, !, merge(T1,[H2|T2],T). merge([H1|T1],[H2|T2],[H2|T]):merge([H1|T1],T2,T),!. merge(X,[],X):-!. merge([],X,X). mergesort([],[]):- !. mergesort([H],[H]):- !. mergesort(L,S):split(L,LL,LR), mergesort(LL,SLL), mergesort(LR,SLR), merge(SLL,SLR,S).
Prolog
6/22
Quicksort
Idea of quicksort
1 2 3 4
select an arbitrary threshold element, split the list into smaller elements, and bigger elements, sort both the lists, append the lists, including threshold element inside.
Quicksort
1 2 3 4 5 6 7 8 9 10 11 12 13
qsort([],[]). qsort([H|T],S):split(H,T,L,R), qsort(L,LS), qsort(R,RS), append(LS,[H|RS],S). split(_,[],[],[]). split(H,[A|X],[A|Y],Z):A =< H, !, split(H,X,Y,Z). split(H,[A|X],Y,[A|Z]):A > H, !, split(H,X,Y,Z).
Prolog
7/22
Loops-repeat-fail
Example loop solutions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
),nl,
), write(X),nl,
loop_find_fail:d(X), write(***found: ),write(X),nl, fail. loop_find_fail. loop_repeat_test:repeat, d(X), write(***found: ),write(X),nl, read(Y), X = Y, write(***end: ),write(X),nl. d(0). d(1). d(2). d(3). d(4). d(5). d(6). d(7). d(8). d(9).
Prolog
8/22
Append-fail-sort
Example sort repeat-test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
asort(L):retractall(list(_)), assert(list(L)), go. go:repeat, list(L), write(L),nl, noimprove(L), !. noimprove(L):- sorted(L). noimprove(L):append(P,[X,Y|K],L), X > Y, retract(list(L)), append(P,[Y,X|K],N), assert(list(N)), fail. sorted([_]). sorted([X,Y|T]):- X=<Y,sorted([Y|T]).
Prolog
9/22
Syntax
Example sort improve-fail
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
sort-improve-fail(L):retractall(list(_)), assert(list(L)), run. run:improve, fail. run:- list(L), write(L),nl. improve:list(L), append(F,[X,Y|R],L), X>Y, retract(list(L)), append(F,[Y,X|R],NL), assert(list(NL)). improve:list(L), \+sorted(L), improve. sorted([_]). sorted([X,Y|T]):- X=<Y,sorted([Y|T]).
Prolog
10/22
p(0):- write(),!. p(N):- write( ),N1 is N-1, p(N1). wl([H|T],D):- !, we(H,D), wl(T,D). wl([],_). we([H|T],D):- !, D2 is D+3, wl([H|T],D2). we(H,D):- nl,p(D),write(H).
?- wl([1,[2,3],4,[5,[6,7]],8],3). 1 2 3 4 5 6 7 8
Antoni Lig eza Prolog 11/22
example
1 2 3 4 5 6
Prolog
12/22
:- dynamic(p/1). :- dynamic(list/1). :- assert(list([])). p(1). p(2). p(3). p(4). p(5). collect(_):p(X), retract(list(T)), assert(list([X|T])), fail. collect(L):- list(L). % coolecti - iterative with retract collecti(L):- collect_iter([],L). collect_iter(T,L):- retract(p(X)), !, collect_iter([X|T],L). collect_iter(L,L).
Prolog
13/22
Prolog
14/22
Creates a list of the instantiations Template gets successively on backtracking over Goal and unies the result with Bag. Succeeds with an empty list if Goal has no solutions. bagof/3
1
Unify Bag with the alternatives of Template, if Goal has free variables besides the one sharing with Template bagof will backtrack over the alternatives of these free variables, unifying Bag with the corresponding alternatives of Template. bagof/3 fails if Goal has no solutions. setof/3
1
Equivalent to bagof/3, but sorts the result using sort/2 to get a sorted list of alternatives without duplicates.
Antoni Lig eza Prolog 15/22
Findall: Example
%%% A Database of facts prac(adam,1400). prac(bogdan,2300). prac(cesiek,2700). prac(damian,2400). prac(eustachy,2600). avg(AV):findall(P,prac(_,P),LP), write(LP),nl, sumlist(LP,SP), length(LP,L), AV is SP/L. pracd(X,P):- prac(X,P),avg(AV), P > AV.
Prolog
16/22
eliminated.
1 2 3 4 5 6 7 8 9 10 11 12
?- length([2,3,4], X). X = 3 ?- sort([4,6,3,1,3,6], X). X = [1, 3, 4, 6] ?- msort([4,6,3,1,3,6], X). X = [1, 3, 3, 4, 6, 6] ?- merge([1,3,4], [2,3], X). X = [1, 2, 3, 3, 4] ?- merge([1,3,4,2], [2,3], X). X = [1, 2, 3, 3, 4, 2] ?- merge_set([1,3,4], [2,3], X). X = [1, 2, 3, 4]
Prolog
17/22
f(X, Y) :- Y is sin(X) + cos(X). even(X) :- X mod 2 =:= 0. %--------------------------------?- maplist(even, [1, 2, 3, 4]). No ?- maplist(even, [2, 4]). Yes ?- maplist(sqrt, [1, 2, 3], X). X = [1.0, 1.41421, 1.73205] ?- maplist(f, [1, 2, 3, 4], X). X = [1.38177, 0.493151, -0.848872, -1.41045] maplist(plus, [1, 2, 3], [1, 2, 3], X). X = [2, 4, 6]
Antoni Lig eza Prolog 18/22
:- op(200,xfy,-). % [a,b,c]-[] % [a,b,c,d,e]-[d,e] % [a,b,c,d,e|T]-[d,e|T] % [a,b,c|T]-T cappend(F-T,T-B,F-B). ?- cappend([a,b,c|T]-T,[d,e|S]-S,L). T = [d, e|S], L = [a, b, c, d, e|S]-S.
Antoni Lig eza Prolog 19/22
The set predicates listed in this section work on ordinary unsorted lists. Note that this makes many of the operations order N^2. For larger sets consider the use of ordered sets as implemented by library ordsets.pl, running most these operations in order N. See section A.15. is_set(+Set) Succeeds if Set is a list (see is_list/1) without duplicates. list_to_set(+List, -Set) Unifies Set with a list holding the same elements as List in the same order. If list contains duplicates, only the first is retained. See also sort/2. Example: ?- list_to_set([a,b,a], X) X = [a,b]
intersection(+Set1, +Set2, -Set3) Succeeds if Set3 unifies with the intersection of Set1 and Set2. Set1 and Set2 are lists without duplicates. subtract(+Set, +Delete, -Result) Delete all elements of set Delete from Set and unify the resulting set with Result. union(+Set1, +Set2, -Set3) Succeeds if Set3 unifies with the union of Set1 and Set2. Set1 and Set2 are lists without duplicates. They n subset(+Subset, +Set) Succeeds if all elements of Subset are elements of Set as well.
Prolog
21/22
Prolog
22/22