【问题标题】:Basis about input and ouput in SWI-PrologSWI-Prolog中输入输出的基础
【发布时间】:2014-06-10 09:41:45
【问题描述】:

谁能告诉我在哪里可以找到关于 Prolog 中输入和输出的好文档?我需要根据一些事实和规则向用户提出一些问题。例如:

best_team(bestteam).

然后在 SWI-Prolog 中提问:你能不能请客最好的团队名称是什么?

所以用户可以写像bestteam 和我Prolog 回答例如:你是对的!

类似的东西。

谢谢。

【问题讨论】:

  • 这个问题我已经问过自己很多次了,所以我自己做了一些文档,available at github

标签: prolog


【解决方案1】:

所示示例可以使用基本 I/O 例程 write/1read/1nl/0 来完成:

best_team(bills).

main :-
    repeat,
    write('Can you guest what the best team name is?'), nl,
    read(X),
    best_team(X),
    write('You are right!'), nl, !.

| ?- main.
Can you guest what the best team name is?
giants.
Can you guest what the best team name is?
bills.
You are right!

yes
| ?-

这可以很容易地扩展为“你错了!”错误响应的消息。一种方法是:

main :-
    repeat,
    write('Can you guest what the best team name is?'), nl,
    read(X),
    (   best_team(X)
    ->  write('You are right!'), nl, !
    ;   write('You are wrong!'), nl, fail
    ).

这将导致:

| ?- main.
Can you guest what the best team name is?
giants.
You are wrong!
Can you guest what the best team name is?
bills.
You are right!

yes
| ?-

如果要输入带有标点符号、大写单词等的输入,则需要使用其他谓词。在这个话题上有一个recent question posted

【讨论】:

  • 非常感谢,Lurker!你能告诉我如何添加错误的回复吗?你对学习这些东西有什么建议吗?链接,书,随便什么。
  • @Checho 根据您的额外问题,我在答案中添加了一些内容。该链接显示了更多关于 SWI Prolog 中一些 I/O 想法的信息。我认为一本关于 Prolog 基础(包括基本 I/O)的好书是 Clocksin & Mellish(第 5 版)的Prolog 编程。此外,在他们的在线手册中浏览 SWI Prolog I/O 谓词,阅读他们的工作,并与一对夫妇一起玩。他们有一些示例,但遗憾的是没有广泛的教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多