比较事实和输出结果

时间:2013-04-10 12:39:06

标签: prolog

我是Prolog的新手,我正在尝试进行测验,用户每个问题都有3个问题和3个选项。在询问每个问题后,将显示3种可能的答案选择。用户键入该问题的答案,并显示下一个问题并继续测验,直到询问所有3个问题并回答。

我接下来要做的是将每个用户答案与该问题和输出的正确答案进行比较,如果用户是对,错或跳过问题。我认为我说我需要将数据库知识中的事实与静态事实进行比较是正确的。我意识到这不是一个真正的问题,但如果有人可以给我建议如何最好地实现我的目标,我将不胜感激。我只是困惑。如果需要,将提供更多信息。

question(1,'What is the fifth planet of our Solar System?').
question(2,'In what year was George Best born?').
question(3,'What is the capital of Austraila?').

possibleAns(1,[mars,jupiter,saturn]).
possibleAns(2,[1945,1946,1948]).
possibleAns(3,[sydney,canberra,melbourne]).

rightAns(1,jupiter).
rightAns(2,1946).
rightAns(3,canberra).

skip(s).

%Confused as to how best to achieve the comparing and output
check_answer(AnsNo,userChoice):-
    rightAns(AnsNo,Choice),
    userAnswer(AnsNo,userChoice)    

getChoice(ChoiceNo,ChoiceList):-
    write('Choose from'),nl,
    write(ChoiceList),nl,
    read(Choice),
    (member(Choice,ChoiceList);skip(Choice)),
    %userAnswer will compare with rightAnswer
    assert(userAnswer(ChoiceNo,Choice)).
getChoice(ChoiceNo,ChoiceList):-
    writeln('Illegal Choice'),
    getChoice(ChoiceNo,ChoiceList).

//check if the question has been asked
//if not, write question
//get users choice and move to next question
get_question(PreviousAsked):-
    question(QNum,Text),
    \+ member(QNum,PreviousAsked),
    write(Text),nl,
    possibleAns(QNum,ChoiceList),
    getChoice(QNum,ChoiceList),
    get_question([QNum|PreviousAsked]).

get_question(_).

start_quiz:-
    get_question([]).

Updated
getChoice(ChoiceNo,ChoiceList):-
    write('Choose from'),nl,
    write(ChoiceList),nl,
    read(Choice),
    (member(Choice,ChoiceList);skip(Choice)),
    assert(userAnswer(ChoiceNo,Choice)).
    (   rightAns(ChoiceNo,Choice)
    ->   write('Right!'),nl;
    write('Wrong,'),nl).

2 个答案:

答案 0 :(得分:2)

如果可能,我们应该避免断言收回,通常我们会得到更简单的程序奖励,更容易理解和调试。

在您的情况下,您最初可以收集问题列表Qs

findall(Q, question(Q,_), Qs)

然后运行循环,直到Qs为空。当用户选择以适当的方式回答时,只删除 的问题。

编辑我只保留问题/ 2,possibleAns / 2,rightAns / 2,并使用此代码

show(Q) :-
    question(Q, T),
    possibleAns(Q, As),
    format('~d: ~s ~w~n', [Q, T, As]).

loop([]).
loop(Qs) :-
    maplist(show, Qs),
    (   (read((Q,Y)),
         select(Q, Qs, Rs),
         rightAns(Q, Y)
        ) -> loop(Rs) ; loop(Qs)
    ).

quiz :-
    findall(Q, question(Q,_), Qs),
    loop(Qs).

我得到了

?- quiz.
1: What is the fifth planet of our Solar System? [mars,jupiter,saturn]
2: In what year was George Best born? [1945,1946,1948]
3: What is the capital of Austraila? [sydney,canberra,melbourne]
|: 1,mars.
1: What is the fifth planet of our Solar System? [mars,jupiter,saturn]
2: In what year was George Best born? [1945,1946,1948]
3: What is the capital of Austraila? [sydney,canberra,melbourne]
|: 1,jupiter.
2: In what year was George Best born? [1945,1946,1948]
3: What is the capital of Austraila? [sydney,canberra,melbourne]
|: 2,1946.
3: What is the capital of Austraila? [sydney,canberra,melbourne]
|: 3,canberra.
true .

答案 1 :(得分:1)

由于您已在数据库中定义rightAns/2,我建议您比较getChoice内的结果,如下所示:

getChoice(ChoiceNo,ChoiceList):-
    write('Choose from'),nl,
    write(ChoiceList),nl,
    read(Choice),
    (member(Choice,ChoiceList);skip(Choice)),
    (   rightAns(ChoiceNo,Choice)
    ->  write('Right!'),nl
    ;   write('Wrong.'),nl).

如果您不喜欢比较getChoice/2内部答案的想法(因为您希望尽可能保持谓词的含义),您可以创建一个简单的规则:

check_answer(AnsNo,userChoice)
    (   rightAns(AnsNo,userChoice)
    ->  write('Right!'),nl
    ;   write('Wrong.'),nl).

我还建议您使用剪切(!)或任何其他机制来防止测验完成后回溯。