2011-10-15 24 views
4

原子清單我需要閱讀的任何行(從USER_INPUT)到原子列表,如:讀線在序言

Example line, which contains any ASCII chars. 

到:

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

我已經走到這一步, :

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

但由於atom_codes的原因,它只適用於單個單詞。 閱讀/ 2也抱怨空間,那麼有沒有辦法做到這一點?

哦,也許再分裂以逗號爲2D-列表,追加點/ exclamationmark /問號,例如:

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

順便說一句:這是SWI-Prolog的。

編輯:找到了解決辦法:

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

不能回答我的問題,因爲我沒有100口碑: -/

回答

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