SHOW:
|
|
- or go back to the newest paste.
| 1 | % Se da sirul a1,..., an. Se cere sa se determine | |
| 2 | % toate subsirurile strict crescatoare ale sirului a. | |
| 3 | ||
| 4 | % Predicatul de inversare a unei liste | |
| 5 | % inverseaza(L:lista, R:lista, Q:lista) | |
| 6 | % L - lista care se primeste | |
| 7 | % R - L inversat | |
| 8 | % Q - lista vida | |
| 9 | % model de flux (i,o,i) | |
| 10 | inverseaza([],R,R):-!. | |
| 11 | inverseaza([H|T],R,Q):- inverseaza(T,R,[H|Q]). | |
| 12 | ||
| 13 | % Predicatul principal | |
| 14 | % ex1(L:lista, Q:lista, R:lista) | |
| 15 | % L - lista initiala | |
| 16 | % Q - lista colectoare | |
| 17 | % R - lista rezultat | |
| 18 | % model de flux (i,i,o) | |
| 19 | ex1([L1|_],[Q1|Q],R):- L1 =< Q1, inverseaza([Q1|Q],R,[]),!. | |
| 20 | ex1([L1|L],[Q1|Q],R):- L1 > Q1, ex1(L,[L1,Q1|Q],R). | |
| 21 | ||
| 22 | % Predicatul ce alege primul element din sublista | |
| 23 | % candidat(L:lista, E:int, R:lista) | |
| 24 | % L - lista initiala | |
| 25 | % E - elementul ce va fi primu din sublista | |
| 26 | % R - lista de dupa E | |
| 27 | % model de flux (i,o,o) | |
| 28 | candidat([H|T],H,T). | |
| 29 | candidat([_|T],E,R):- candidat(T,E,R). | |
| 30 | ||
| 31 | % Predicatul ce alege al doilea element din sublista | |
| 32 | % c2(L:lista, E:int, R:lista) | |
| 33 | % L - lista initiala | |
| 34 | % E - elementul ce va fi primu din sublista | |
| 35 | % R - lista de dupa E | |
| 36 | % model de flux (i,o,o) | |
| 37 | c2([H|T],H,T):-!. | |
| 38 | ||
| 39 | % Predicatul de apel | |
| 40 | % apel(L:lista,Q:lista) | |
| 41 | % L - lista de intrare | |
| 42 | % Q - lista rezultat | |
| 43 | % model de flux (i,o) | |
| 44 | apel(L,Q):- candidat(L,E1,L2),c2(L2,E2,L3),E1 < E2, ex1(L3,[E2,E1],Q). | |
| 45 | ||
| 46 | % Predicatul de start | |
| 47 | % apel(L:lista,R:lista) | |
| 48 | % L - lista de intrare | |
| 49 | % R - lista de liste rezultat | |
| 50 | % model de flux (i,o) | |
| 51 | start(L,R):- findall(Q, apel(L,Q), R). |