Read Input till quit statement prolog

2019-08-16 06:35发布

So I'm coding a chat Bot and I need to keep taking input from the user as long as they don't type 'quit.', but if they do I must do a certain recap of what the user asked and then return true , I'm having trouble with the predicate that will keep reading the input till quit is typed , Can anyone help me? Thanks

标签: prolog
1条回答
劫难
2楼-- · 2019-08-16 07:22

The question is worded rather generally, so here's a description of how to make it work in Prolog semi-pseudo-code:

user_input :-
    repeat,
    read_a_line(Line),
    (   Line = quit  % You might have another way to check this depending...
    ->  write('Finished'), nl,
        !, true
    ;   do_something_with(Line),
        fail
    ).

The way the repeat-fail loop works is that you want to fail to continue to do your loop. Then for exiting the loop, you want to succeed with a cut.

查看更多
登录 后发表回答