Choosing relations at random using findall
- From: mayo_matt@xxxxxxxxxxx
- Date: 3 Jul 2006 11:23:31 -0700
Hi again,
I have written this program that selects 3 items randomly from a small
database of assertions and displays the 3 selected items onscreen. The
code makes sense to me, but only writes out a single item. I have
altered this code so many times to no avail that I hope a fresh set of
eyes may give me a hand. Anyone with the time to look it over and make
a suggestion, I would appreciate the help. Thanks in advance.
% Courses database
course('COMP456','Artifical Intelligence','TeacherName1').
course('COMP492','Data and the Semantic Web','TeacherName2').
course('SCIE326','Scientific Reasoning','TeacherName3').
course('COMP200','Introduction to Computers','TeacherName4').
course('MATH265','Intro to Calculus I','TeacherName5').
course('MATH309','Discrete Mathematics','TeacherName6').
random_course :- findall(X,course(X,_,_),List),
N is 3,
setup(N,List).
setup(N,List) :- length(List,Length),
RandomNum is random(Length-1),
RandomNum1 is RandomNum+1,
write('Random Number:'), write(RandomNum1), nl,
write('Courses: '),
write(List), nl,
write('Total Number of Courses: '), write(Length),
nl,
write('Number of Courses to Select: '),
write(N), nl,
pick_course(RandomNum1,List,N).
pick_course(RandomNum,List,0) :- !.
pick_course(RandomNum,List,N) :- element_at(Course,List,RandomNum), nl,
write('Selected Course: '), write(Course),
length(List,Length),
RandomNum is random(Length-1),
RandomNum1 is RandomNum+1,
% Why won't it recurse properly???
N1 is N-1,
write(N1),
pick_course(RandomNum1,List,N1).
element_at(X,[X|_],1) :- !.
element_at(X,[_|L],K) :- K > 1,
K1 is K-1,
element_at(X,L,K1).
.
- Follow-Ups:
- Re: Choosing relations at random using findall
- From: mayo_matt
- Re: Choosing relations at random using findall
- Prev by Date: Re: Splitting word based on syllables
- Next by Date: Re: Splitting word based on syllables
- Previous by thread: Splitting word based on syllables
- Next by thread: Re: Choosing relations at random using findall
- Index(es):