Prolog - 写出事实并阅读用户输入 [英] Prolog - Write out facts and reading a users input

查看:51
本文介绍了Prolog - 写出事实并阅读用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Prolog 很陌生,并且在理解它时遇到了一些麻烦.我有一些名为问题"的事实,我希望先将这些事实打印给用户,然后让他们输入一个值,然后读取该值并稍后使用.

I am quite new to Prolog and have had some trouble understanding it. I have some facts named 'problem' I wish to first print out these facts to the user and then ask them to input a value, this value is then read and used later.

根据我目前的理解,最好使用 forall 打印出这些事实,然后使用 read 读取输入的值,但我在实现这一点时遇到了一些问题.这是我到目前为止所拥有的,任何解释将不胜感激

From my understanding thus far, it would be best to use a forall to print out these facts and then use read to read the value inputted, but I am having some issue implementing this. Here is what I have so far, any explanation would be appreciated

我的问题:我如何读取用户关于问题的输入并将其应用到变量中以备后用?

My question: How do I read in the input from the user regarding the problem and apply that into a variable for later use?

tellMeYourProblem:-
forall(problem(P), 
writeln(P)),
answer = read(X),


problem('1').
problem('2').
problem('3').
problem('4').
problem('5').
problem('6').
problem('7').
problem('8').
problem('9').
problem('10').

推荐答案

注意:此答案使用 SWI-Prolog.

Note: This answer uses SWI-Prolog.

我如何读入用户关于问题的输入?

How do I read in the input from the user regarding the problem?

您已经使用 read(X) 这样做了,但是 read/1 读取术语(术语以句点结尾)并且您可能想要读取字符.如果您使用 SWI-Prolog,请查看 原始字符 I/O 用于读取字符和 对字符串进行操作的谓词读取字符串.

You are doing that already with read(X), however read/1 reads terms (terms end with periods) and you probably want to read characters. If you are using SWI-Prolog take a look at Primitive character I/O for reading characters and Predicates that operate on strings for reading strings.

如何将其应用到变量中以备后用?

How do I apply that into a variable for later use?

在文本级别与用户进行基本 I/O 时,REPL 是一个很好的开始方式.添加REPL有点复杂,所以我给你代码.

When doing basic I/O with a user at a text level, a REPL is a good way to start. Adding a REPL is a bit more complicated so I will give you the code.

tellMeYourProblem :-
    output_problems,
    read_input.

output_problems :-
    forall(problem(P),
    writeln(P)).

read_input :-
    repeat,
    read_string(user_input, "\n", "\r\t ", _, Line),
    process_input(Line).

process_input(Line) :-
    string(Line),
    atom_number(Line, N),
    integer(N),
    do_something_with(Line),
    fail.
process_input("quit") :-
    write('Finished'), nl,
    !, true.

do_something_with(X) :-
    writeln(X).

problem('1').
problem('2').
problem('3').
problem('4').
problem('5').
problem('6').
problem('7').
problem('8').
problem('9').
problem('10').

还有 Prolog,样式是使用 蛇形套管 所以 tellMeYourProblem 应该是 tell_me_your_problem.

Also with Prolog, the style is to use snake casing so tellMeYourProblem should be tell_me_your_problem.

通常在 Prolog 中,REPL 使用 ->/2, (Read Input until quit statement Prolog) ,但我更改了它以添加更多 guard 语句,以便退出条件起作用,例如

Normally in Prolog a REPL is done with ->/2, (Read Input till quit statement Prolog) , but I changed this to add more guard statements so that the exit condition would work, e.g.

string(Line),
atom_number(Line, N),
integer(N)

或者把守卫放在头上,例如

or putting the guard in the head, e.g.

process_input("quit")

当对屏幕和键盘进行 I/O 操作时,我们的想法是使用 stdInstdOut 但对于键盘 SWI-Prolog 使用 user_input 代替.请参阅:输入和输出

When doing I/O to a screen and keyboard, the thought is to use stdIn and stdOut but for the keyboard SWI-Prolog uses user_input instead. See: Input and output

毕竟 REPL 的所有样板代码是您寻找的下一部分,即对输入值执行某些操作,在这种情况下只需将其打印出来.

After all of the boiler plate code for the REPL is the next part you seek which is to do something with the input value, in this case just print it out.

do_something_with(X) :-
    writeln(X).

这篇关于Prolog - 写出事实并阅读用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆