Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read line to atomic list in prolog

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 :-/

like image 701
nonchip Avatar asked Nov 05 '22 12:11

nonchip


1 Answers

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], '.'].
like image 161
BLUEPIXY Avatar answered Nov 15 '22 10:11

BLUEPIXY