Jump to content


Prolog help please


3 replies to this topic

#1 clash

    New Member

  • Members
  • Pip
  • 1 posts

Posted 26 October 2006 - 10:56 PM

Hey guys, just starting AI and doing a simple program in prolog. In the below code (i know its messy etc) but userAns is a dynamic list holding basically a question, the users responce to that question and whether or not the user got it right/wrong or skipped it.

But in my 2nd main function (in bold) writeln(userAns), doesn't work. It doesn't seem to recongise userAns as the list. Can anyone help please ?

I assume theres some sort of "out of scope" issue that i'm just not aware of ...

:- dynamic userAns/3.



%Questions 


q('The sky is ? ',['1. brown','2. blue '], 2).

q('sugar is ? ',['1. sour','2. sweet'], 2).



main :-

       q(Question,PossibleAnswers,RightAnswer),

       writeln('Answer the following question using the question number or 0 to skip.'),

       writeln(Question),

       writeln(PossibleAnswers),

       read(Ans),

       result(q(Question,PossibleAnswers,RightAnswer),Ans),

       fail.



main :-

	writeln('Your finished, results are as follows'),

	[B]writeln(userAns),[/B] <--Doesn't seem to recongise as the dynamic list. 

	fail.

main.



result(q(Question,PossibleAnswers,Ans),Ans):-

       assertz(userAns(Question,correct,Ans)),!.



result(q(Question,PossibleAnswers,_),0):-

       assertz(userAns(Question,skipped,Ans)),!.



result(q(Question,PossibleAnswers,_),_):-

       assertz(userAns(Question,wrong,Ans)),!.


#2 pater

    Valued Member

  • Members
  • PipPipPip
  • 117 posts

Posted 27 October 2006 - 07:28 PM

Uh, Oh... Been a very long time since I last used Prolog...

Your Problem seems to be that userAns is not a list, it's a predicate.
The System looks for userAns/0 when you attempt to print it, which obviously doesn't exist. You have to iterate over all matches to userAns/3 and write out those strings.

I couldn't really find the exact solution yet, since as said, haven't used it for quite a while.

#3 Devoto

    Member

  • Members
  • PipPip
  • 48 posts

Posted 27 October 2006 - 10:05 PM

Funny, now I am studying lists in prolog. Maybe I can help you in a near future!!!

#4 Trap D

    Member

  • Members
  • PipPip
  • 55 posts

Posted 29 October 2006 - 08:57 AM

userAns is an atom. userAns(X,Y,Z) is a tuple.
You should do that :

	forall(userAns(A,B, C), (write(A), write(' '), write(B), write(' '), writeln(C))), 







1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users