%% algwriter(A),write(A). multiply(A,B,C) :- C is A*B. divide(A,B,C) :- C is A/B. add(A,B,C) :- C is A+B. subtract(A,B,C) :- C is A-B. head(A,B) :- A=[B|_]. tail(A,B) :- A=[_|B]. wrap(A,B) :- B=[A]. algwriter(Na) :- makerandomlist(3,[],R1),makerandomlist(3,[],R2),wrap(R1,Nb1),wrap(R2,Nb2),append(Nb1,Nb2,Nb3),randomfns(8,Nb3,Na),!. makerandomlist(0,A,A). makerandomlist(A,C1,C) :- not((=(A,0))),random(R),multiply(R,5,R1),ceiling(R1,N1),wrap(N1,N2),append(C1,N2,Nb3),subtract(A,1,D),makerandomlist(D,Nb3,C). randomfns(0,A,A). randomfns(A,B,C) :- not((=(A,0))),randomlist(B,Na1),randomlist(B,Na2),randomfn(Na1,Na2,Nb),wrap(Nb,Nb2),append(B,Nb2,Nb3),%%tail(B,T), subtract(A,1,D),randomfns(D,Nb3,C). randomlist(B,Na) :- random(R),length(B,Bl),multiply(R,Bl,N),ceiling(N,N1),getitemn(N1,B,Na). getitemn(0,_A,[]). getitemn(1,B,C) :- head(B,C). getitemn(A,B,C) :- not((=(A,1))),tail(B,T),subtract(A,1,D),getitemn(D,T,C). randomfn(A1,A2,B) :- random(R),multiply(R,9,N),ceiling(N,N1),fna(N1,A1,A2,B). fna(1,A1,_A2,B) :- reverse(A1,[],B). fna(2,A1,_A2,B) :- sort0(A1,B). fna(3,A1,_A2,B) :- findall(dividebyfour,A1,[],B). fna(4,A1,A2,B) :- append1(A1,A2,B). fna(5,A1,_A2,B) :- findall(plusone,A1,[],B). fna(6,A1,_A2,B) :- findall(plustwo,A1,[],B). fna(7,A1,_A2,B) :- findall(multiplybytwo,A1,[],B). fna(8,A1,A2,B) :- minus1(A1,A2,B). fna(9,A1,A2,B) :- intersection1(A1,A2,[],B). reverse([],L,L). reverse(L,M,N) :- head(L,H),tail(L,T),wrap(H,H1),append(H1,M,O),reverse(T,O,N). sort0(L,N) :- sort1(L,[],N). sort1([],L,L). sort1(L,M1,N) :- not((=(L,[]))),head(L,H),tail(L,T),maximum(T,H,M2,[],R),wrap(M2,M3),append(M1,M3,M4),sort1(R,M4,N). maximum([],L,L,R,R). maximum(L,M1,N,R1,R2) :- not((=(L,[]))),head(L,H),tail(L,T),(>=(M1,H)->(=(M2,M1),wrap(H,H2),append(R1,H2,R3));(=(M2,H),wrap(M1,M12),append(R1,M12,R3))),maximum(T,M2,N,R3,R2). map(_F,[],L,L). map(F,L,M1,N) :- not((=(L,[]))),head(L,H),tail(L,T),functor(A,F,3),arg(1,A,M1),arg(2,A,H),arg(3,A,M2),A,map(F,T,M2,N). findall(_F,[],L,L). findall(F,L,M1,N) :- not((=(L,[]))),head(L,H),tail(L,T),functor(A,F,2),arg(1,A,H),arg(2,A,M2),(A->((wrap(M2,M3),append(M1,M3,M4)));(=(M1,M4))),findall(F,T,M4,N). plusone(A,C) :- add(A,1,C). plustwo(A,C) :- add(A,2,C). multiplybytwo(A,C) :- multiply(A,2,C). dividebyfour(A,C) :- divide(A,4,C). intersection1([],_A,L,L). intersection1(L1,L2,L3a,L3) :- head(L1,I1),tail(L1,L4),intersection2(I1,L2,[],L5),append(L3a,L5,L6),intersection1(L4,L2,L6,L3). intersection2(_A,[],L,L). intersection2(I1,L1,L2,L3) :- head(L1,I1),tail(L1,L4),wrap(I1,I11),append(L2,I11,L5),intersection2(I1,L4,L5,L3). intersection2(I1,L1,L2,L3) :- head(L1,I2),tail(L1,L4),not((=(I1,I2))),intersection2(I1,L4,L2,L3). append1(B,C,A) :- append(B,C,A). minus1(L,[],L). minus1(L1,L2,L3) :- head(L2,I1),tail(L2,L5),delete2(L1,I1,[],L6),minus1(L6,L5,L3). delete2([],_A,L,L). delete2(L1,I1,L2,L3) :- head(L1,I1),tail(L1,L5),delete2(L5,I1,L2,L3). delete2(L1,I1,L2,L3) :- head(L1,I2),tail(L1,L5),not((=(I1,I2))),wrap(I2,I21),append(L2,I21,L6),delete2(L5,I1,L6,L3).