如何将用户输入写入列表序言 [英] How to Write User Input to a List Prolog

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

问题描述

我试图将用户输入写入列表,但是遇到两个问题.使用此代码

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([]).

我能够从用户那里得到症状,但是输出显示第一个用户输入为Head,其他显示为尾.我该如何使Head出现症状",并且仅将用户输入的内容添加到Tail中?其次,当我将停止"更改为完成"时,程序不再停止吗?

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?

推荐答案

有效

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

用途
1. ==/2 代替 dif/2 .
2. read_string/5 代替 write/1 代替->/2 而不是单独的条款

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.

您可以将退出单词的值更改为"Stop"(停止)以外的值,甚至可以使用单个字符,例如".如果愿意的话.

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 (!).

示例运行:

?- 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?

您要使用户输入头部,并在尾部输入以下内容.此示例的技巧是将列表构造函数|/2放在递归调用之后,例如

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?

由于Done以大写字母开头,因此它是导致问题的Prolog变量. stop以小写字母开头,是一个原子,因此在进行比较时可以正常工作.

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?

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

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

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

X = "Hello".

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

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

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

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