Author |
Message |
Chairil Willi #1 / 6
|
 Stuck!!
Hi everyone. Ive been slaving over this assignment for little over a day. I basically create two databases (tables), each with a set of specific data. I then have to cross-reference the data, and use assert to creat e new database. The first table contains names of pupils with the code of the classes theyre in, the second contains the codes and the corresponding names of the classes. I need to create a third table containing the pupils names and their classes. this is what i came up with so far: pupil(maria,[1,2,3]). pupil(jose,[1,3,5]). pupil(pedro,[1,3,4]). pupil(marcus,[2,3]). class(1,paradigms). class(2,algorithmos). class(3,labpro). class(4,sociology). class(5,geography). generate:- pupil(A,L3), flow(A,L3,L1), assert(signed_up(A,L1)), fail. flow(A,[],_). flow(A,[H|T],Lis):- class(H,N), flow(A,T,L), append(L,[N],Lis). The problem is it seems to trigger an infinite loop. I cant understand why, any help will be sincerely appreciated. And oh, I wouldnt be posting here if it wasnt urgent. I try to make my homework...:)
|
Thu, 06 May 2004 08:38:47 GMT |
|
 |
(complete unknown #2 / 6
|
 Stuck!!
Quote: > The first table contains names of pupils with the code of the > classes theyre in, > the second contains the codes and the corresponding names > of the classes. > I need to create a third table containing the pupils' names and > their classes.
For example, Quote: > pupil(maria,[1,2,3]). > pupil(jose,[1,3,5]). > pupil(pedro,[1,3,4]). > pupil(marcus,[2,3]). > class(1,paradigms). > class(2,algorithmos). > class(3,labpro). > class(4,sociology). > class(5,geography).
would give subjects(maria,[paradigms,algorithmos,labpro]). subjects(jose,[paradigms,labpro,geography]). subjects(pedro,[paradigms,labpro,sociology]). subjects(marcus,[algorithmos,labpro]). If you were given a list of class codes and a list of class names, how would you test to see if the two lists were in correspondence?
|
Thu, 06 May 2004 17:09:01 GMT |
|
 |
Katiusci #3 / 6
|
 Stuck!!
I think the mistake is here: flow(A,[],_). flow(A,[H|T],Lis):- class(H,N), flow(A,T,L), append(L,[N],Lis). First of all, try to switch the last two assignement. Katiuscia.
|
Fri, 07 May 2004 15:24:59 GMT |
|
 |
Fergus Henders #4 / 6
|
 Stuck!!
Quote:
>pupil(maria,[1,2,3]). >pupil(jose,[1,3,5]). >pupil(pedro,[1,3,4]). >pupil(marcus,[2,3]). >class(1,paradigms). >class(2,algorithmos). >class(3,labpro). >class(4,sociology). >class(5,geography). >generate:- > pupil(A,L3), > flow(A,L3,L1), > assert(signed_up(A,L1)), > fail. >flow(A,[],_). >flow(A,[H|T],Lis):- > class(H,N), > flow(A,T,L), > append(L,[N],Lis). >The problem is it seems to trigger an infinite loop. >I can't understand why, any help will be sincerely appreciated.
The program has a mode error. It would help if you annotated each predicate with the intended mode. For example, pupil/2 is a set of ground facts, so it generates the values of both of its arguments. % mode pupil(out, out). Now flow/3 gets called from generate/0 with the first two arguments ground (from the call to pupil/2), but the third argument free. So, we can reasonably assume that it must be intended to produce the value of that argument. % mode flow(in, in, out). Now look at the clauses for flow/3. The second clause is fine, as you can see by stepping through it and seeing how each variable gets bound: flow(A,[H|T],Lis):- % flow(in, in, out):- % A, H, T all ground class(H,N), % class(in, out) % N ground flow(A,T,L), % flow(in, in, out), % L ground append(L,[N],Lis). % append(in, in, out). % Lis ground But the first clause flow(A, [], _). leaves the third argument as a free variable. This will cause problems. After a recursive call to flow/3, you will end up calling append(_, [N], _), which has an infinite number of solutions, and so when you call fail/0 from generate/0, it will try to backtrack over an infinite set of solutions, which will lead to an infinite loop. If you find doing this kind of mode analysis by hand tedious, then you may prefer programming in Mercury, where the compiler does it for you ;-) --
The University of Melbourne | of excellence is a lethal habit" WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
|
Fri, 07 May 2004 17:39:11 GMT |
|
|
|