:- use_module(library(lists)).
:- nl, nl, write('  Inditas: gen(K,L).'), nl.
:- nl, nl, write('     Pl:   gen(4,11). '),nl,nl.

%-------------------- a szukseges segedfuggvenyek ------------------------

between(N,M,N) :- N =< M.
between(N,M,I) :- N < M, N1 is N+1, between(N1,M,I).


%insert(Elem, InputLista, OutputLista) : 
%             amennyiben Elem eleme az InputListanak, false az eredmeny, 
%             egyebkent pedig OutputLista=Inputlista+Elem
insert(E,[],[E]).
insert(E,[X|L0],[E,X|L0]):- E>X.
insert(E,[X|L0],[X|L1]):- X>E, insert(E,L0,L1).


kiir([]).
kiir([H|B]):- write(H), nl, kiir(B).

%------------------------ Golomb vonalzo ---------------------------------

%Elem k-sok legeneralasa:
elemgen(K,L0,L,H):- K=1, !, L>=L0, H=[L].
elemgen(K,L0,L,[E|H]):- K1 is K-1, 
                        between(L0,L,E), E1 is E+1, 
                        elemgen(K1,E1,L,H). 
elemgen(K,L,[0|H]) :- elemgen(K,1,L,H).


%A H-ban levo kulonbozo elemekbol keszitett elemparok eloallitasa:
parosok(H,P) :- findall((X,Y),(member(X,H),member(Y,H),X>Y),P).


%Annak vizsgalata, hogy a legeneralt elemparok mind kulonbozo tavolsagokat
%reprezentalnak-e:
kulonbsegek([],[]).
kulonbsegek([(X,Y)|P],L) :- D is X-Y, 
                            kulonbsegek(P,L0), 
			    insert(D,L0,L).
%Az insert no-t ad, ha olyan elemmel akarjuk boviteni 
%a listat, ami mar benne van - lasd fent!


jolistak(K,L):- elemgen(K,L,H),
                parosok(H,P),
		kulonbsegek(P,_),
                tell('jolistak.txt'),
		write(H),
		nl,
		fail.
jolistak(_,_).


gen(K,L):- tell('jolistak.txt'), jolistak(K,L), told.

%------------ A Golomb vonalzorol bovebben <a href="http://www.research.ibm.com/people/s/shearer/grtab.html">itt</a> olvashatsz --------------
