如何将用户控制台输入到 Prolog 列表中 [英] How to get user console input into a Prolog list

查看:15
本文介绍了如何将用户控制台输入到 Prolog 列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户输入写入列表,但遇到了两个问题.使用此代码

getInput([症状|列表]):-writeln('输入元素:'),阅读(症状),差异(症状,停止),获取输入(列表).获取输入([]).

我能够从用户那里得到症状,但输出显示第一个用户输入作为头部,其他作为尾部.如何使头部症状"并且仅将用户输入广告到尾部?其次,当我将停止"更改为完成"时,程序不再停止?

所以有人告诉我完成"不起作用,因为它是大写的.如果我想使用大写单词,我该怎么做?

解决方案

这可行

get_symptoms(症状) :-write('输入症状:'),读取字符串(用户,
",
",_,响应),(响应 == 停止"->症状 = [];get_symptoms(症状0),症状 = [响应|症状 0]).

用途
1. ==/2 而不是 dif/2.
2. read_string/5 而不是 read/1 以便输入字符串.
3. write/1 而不是 writeln/1 以便输入与提示位于同一行.
4. ->/2 代替单独的子句.

您可以将退出词的值更改为Stop"以外的其他值,甚至可以使用."等单个字符.如果你喜欢.

另请注意,这不需要剪切 (!).

示例运行:

?- get_symptoms(List).进入症状:停止列表 = [].?- get_symptoms(列表).进入症状:A进入症状:停止列表 = [A"].?- get_symptoms(列表).输入症状:a进入症状:A输入症状:一行有一些空格输入症状:注意末尾不需要句号进入症状:停止List = ["a", "A", "A line with some spaces", "注意末尾不需要句点"].

<小时><块引用>

如何使头部症状"并且仅将用户输入添加到尾部?

您想让用户输入头部并将以下条目作为尾部.此示例的技巧是将列表构造函数 |/2 放在递归调用之后,例如

 get_symptoms(Symptoms0), % 递归调用症状 = [Response|Symptoms0] % 列表构造函数

<块引用>

其次,当我将停止"更改为完成"时,程序不再停止?

由于 Done 以大写字母开头,因此它是导致问题的 Prolog 变量.stop 以小写字母开头并且是一个原子,因此可以按预期进行比较.

<块引用>

如果我想使用大写单词,我该怎么做?

您可以使用 read/1 这需要用户在输入值时输入",例如

?- 读取(X).|:你好".X =你好".

read/1 读取一个术语,因此需要用户不仅要添加双引号,还要以句点结尾.使用 read_string/5 允许用户输入数据,因为他们会期望.输入将被读取并存储为字符串.如果要转换数据,则存在对 字符串

I am trying to write the user input into a list but am running into two problems. Using this code

getInput([Symptom|List]):-
    writeln('Enter Element:'),
    read(Symptom),
    dif(Symptom,stop),
    getInput(List).
getInput([]).

I am able to get symptoms from the user but the output shows the first user input as the Head, and the others as the tail. How do I make the Head "Symptoms" and only ad the user input to the Tail? Secondly, when I change "stop" to "Done" the program no longer stops?

So I was told "Done" doesn't work because it is capitalized. If I want to use a capital word, how would I do that?

解决方案

This works

get_symptoms(Symptoms) :-
    write('Enter Symptom: ' ),
    read_string(user, "
", "
", _, Response),
    (
        Response == "Stop"
    ->
        Symptoms = []
    ;
        get_symptoms(Symptoms0),
        Symptoms = [Response|Symptoms0]
    ).

Uses
1. ==/2 instead of dif/2.
2. read_string/5 instead of read/1 so that strings can be entered.
3. write/1 instead of writeln/1 so that input is on same line as prompt.
4. ->/2 instead of separate clauses.

You can change the value of the exit word to something other than "Stop", you can even use single characters like "." if you like.

Notice also that this does not require a cut (!).

Example runs:

?- get_symptoms(List).
Enter Symptom: Stop
List = [].

?- get_symptoms(List).
Enter Symptom: A
Enter Symptom: Stop
List = ["A"].

?- get_symptoms(List).
Enter Symptom: a
Enter Symptom: A
Enter Symptom: A line with some spaces
Enter Symptom: Notice that a period is not needed at the end
Enter Symptom: Stop
List = ["a", "A", "A line with some spaces", "Notice that a period is not needed at the end"].


How do I make the Head "Symptoms" and only add the user input to the Tail?

You want to make the user input the head and have the following entries as the tail. The trick for this example is to put the list constructor |/2 after the recursive call, e.g.

    get_symptoms(Symptoms0),           % Recursive call
    Symptoms = [Response|Symptoms0]    % List constructor

Secondly, when I change "stop" to "Done" the program no longer stops?

Since Done starts with a capital letter it is a Prolog variable which is causing the problem. stop starts with a lower case letter and is an atom so works as expected with the compare.

If I want to use a capital word, how would I do that?

You can use read/1 which requires the user to input "" when entering the value, e.g.

?- read(X).
|: "Hello".

X = "Hello".

but read/1 reads a term so requires the user to not only add the double quotes but end with a period. Using read_string/5 allows the user to enter data as they would expect. The input will be read and stored as a string. If the data is then to be converted there are predicates that operate on strings

这篇关于如何将用户控制台输入到 Prolog 列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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