read line to atomic list in prolog

感情迁移 提交于 2019-12-06 05:07:16

问题


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

Example line, which contains any ASCII chars.

into:

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

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],'.']

btw: that's SWI-prolog.

EDIT: found the solution:

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

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


回答1:


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.

DEMO

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


来源:https://stackoverflow.com/questions/7777991/read-line-to-atomic-list-in-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!