Tuesday, October 13, 2009

List.pro singleton Lab4 part4

rnprint(0).
rnprint(A) :-
random(0,10,R),
write(R),nl,
B is A-1,
rnprint(B).

rnlist(0,[]).
rnlist(N,X) :-
R is N-1,
rnlist(R,A),
random(0,10,B),
append(A,[B],X).


%counts occurrences of the given number in the generated list


count(_,[],0).
count(A,[H|T],B) :-
A = H,
count(A,T,K),
B is K + 1.
count(A,[H|T],B) :-
count(A,T,B).

%counts number of digits with single occurrences

single_count([],0).
single_count(A,K) :-
single_2(A,10,C),
K is C.

single_2(_,0,0).
single_2(A,B,K) :-
D is B - 1,
count(D,A,T), T = 1,
single_2(A,D,C),
K is C + 1.
single_2(A,B,K) :-
D is B - 1,
single_2(A,D,K).