如何将从输入中读取的字符串转换为Prolog中的列表列表 [英] How to convert a string read from input to a list of lists in prolog

查看:88
本文介绍了如何将从输入中读取的字符串转换为Prolog中的列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,在序言中将玛雅语转换为阿拉伯数字,反之亦然.尽管我仍然遇到一些麻烦,但我设法使它大部分都能正常工作.我只是想知道如何阅读例如:

I'm trying to write a program that converts from Mayan to Arabic numerals and vice versa in prolog. Although I'm still running into some trouble I've managed to get it mostly working. I'm just wondering how if I read for example:

....| 0 .| |||

从用户输入中,如何将其转换为这样的列表:

from a user input, how can I convert it to a list like this:

L = [ ['.','.','.','.','|'], [0], ['.','|'], ['|','|','|'] ]

我写了一个从L到阿拉伯数值的算法,但是我不知道如何将字符串转换成列表.

I have an algorithm written getting from L to the arabic value, but I have no clue how to convert that string into a list.

推荐答案

看看

Take a look at this answer. The code I've written there splits a Prolog string on spaces to generate a list of atoms. With a small modification, you can alter the code to create strings instead of atoms. Here is the relevant code from the previous post, with the necessary modification for your case:

data([A|As]) --> 
    spaces(_), 
    chars([X|Xs]), 
    {string_to_list(A, [X|Xs])},  %% Using string_to_list/2 instead
    spaces(_), 
    data(As).
data([]) --> [].

chars([X|Xs]) --> char(X), !, chars(Xs).
chars([]) --> [].

spaces([X|Xs]) --> space(X), !, spaces(Xs).
spaces([]) --> [].

space(X) --> [X], {code_type(X, space)}. 
char(X) --> [X], {\+ code_type(X, space)}.

在您的示例中,您将获取一个包含如"....| 0 .| |||"这样的示例的Prolog字符串,并使用内置的phrase/2来运行上述代码,如下所示:

In your example, you'd take a Prolog string containing your example like "....| 0 .| |||" and run the above code using the built-in phrase/2, like so:

?- phrase(data(NumeralList), "....| 0 .| |||").
NumeralList = ["....|", "0", ".|", "|||"]

请注意,我已经在 SWI-Prolog 上进行了测试,并且可以使用,但是如果您使用的是不同的Prolog实现,它可能不支持DCG或我使用的内置插件.

Note that I've tested this on SWI-Prolog and it works, but if you're using a different Prolog implementation, it mightn't support DCGs or the built-ins I've used.

如果您得到的结果恰好是 ,如上面的L所述,则可以进一步修改代码以直接在data子句(删除子目标{string_to_list(A, [X|Xs])},),并将最后一个谓词char更改为以下内容:

If you were after a result which is precisely like what you've described as L above, you can modify the code further to return the list [X|Xs] directly in the data clause (removing the sub-goal {string_to_list(A, [X|Xs])},), and change the last predicate char to the following:

char(C) --> [X], {\+ code_type(X, space), atom_codes(C,[X])}.

运行此操作可获得:

?- phrase(data(L), "....| 0 .| |||").
L = [['.', '.', '.', '.', '|'], ['0'], ['.', '|'], ['|', '|', '|']]

编辑:根据要求,以下是修改后的代码,可完整生成上述结果:

As requested, here is the modified code which generates the above result in full:

data([[X|Xs]|As]) --> 
    spaces(_), 
    chars([X|Xs]), 
    spaces(_), 
    data(As).
data([]) --> [].

chars([X|Xs]) --> char(X), !, chars(Xs).
chars([]) --> [].

spaces([X|Xs]) --> space(X), !, spaces(Xs).
spaces([]) --> [].

space(X) --> [X], {code_type(X, space)}. 
char(C) --> [X], {\+ code_type(X, space), atom_codes(C,[X])}.

这篇关于如何将从输入中读取的字符串转换为Prolog中的列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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