在序言中读取行到原子列表 [英] read line to atomic list in prolog

查看:56
本文介绍了在序言中读取行到原子列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将任何行(来自 user_input)读入原子列表,例如:

I need to read any line (from user_input) into an atomic list, e.g.:

Example line, which contains any ASCII chars.

进入:

[Example,'line,',which,contains,any,ASCII,'chars.']

到目前为止我得到了什么:

what I've got so far:

read_line_to_codes(user_input, Input),
atom_codes(IA,Input),
atomic_list_concat(AlistI,' ',IA).

但是由于atom_codes,这只适用于单个单词.read/2 也抱怨空格,那么有没有办法做到这一点?

but that only works w/ single words, because of atom_codes. read/2 also complains about spaces, so is there any way to do this?

哦,然后可能会以逗号分割成二维列表,附加点/感叹号/问号,例如:

oh and maybe then splitting at comma into 2d-lists, appending the dot/exclamationmark/questionmark, e.g.:

[[Example,line],[which,contains,any,ASCII,chars],'.']

顺便说一句:那是 SWI 序言.

btw: that's SWI-prolog.

找到解决方案:

read_line_to_codes(user_input, Input),
string_to_atom(Input,IA),
atomic_list_concat(AlistI,' ',IA),

无法回答我自己的问题,因为我没有 100 声望 :-/

can't answer my own question because i don't have 100 reputation :-/

推荐答案

input_to_atom_list(L) :-
    read_line_to_codes(user_input, Input),
    string_to_atom(Input,IA),
    tail_not_mark(IA, R, T),
    atomic_list_concat(XL, ',', R),
    maplist(split_atom(' '), XL, S),
    append(S, [T], L).

is_period(.).
is_period(?).
is_period(!).

split_atom(S, A, L) :- atomic_list_concat(XL, S, A), delete(XL, '', L).

%if tale is ? or ! or . then remove
%A:Atom, R:Removed, T:special mark
tail_not_mark(A, R, T) :- atom_concat(R, T, A), is_period(T),!. 
tail_not_mark(A, R, '') :- A = R.

演示

1 ?- input_to_atom_list(L).
|: Example line, which contains any ASCII chars.
L = [['Example', line], [which, contains, any, 'ASCII', chars], '.'].

这篇关于在序言中读取行到原子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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