从您的错误消息来看,显然您正在使用 SWI-Prolog。然后你可以使用它的库支持:
?- read_line_to_codes(user_input,Cs), atom_codes(A, Cs), atomic_list_concat(L, ' ', A).
|: hello world !
Cs = [104, 101, 108, 108, 111, 32, 119, 111, 114|...],
A = 'hello world !',
L = [hello, world, !].
为了更直接地处理“字符串”(实际上,字符串是代码列表),我在以下人员的帮助下构建了我的拆分器string http://www.swi-prolog.org/pldoc/doc_for?object=dcg_basics%3astring//1//1
:- use_module(library(dcg/basics)).
%% splitter(+Sep, -Chunks)
%
% split input on Sep: minimal implementation
%
splitter(Sep, [Chunk|R]) -->
string(Chunk),
( Sep -> !, splitter(Sep, R)
; [], {R = []}
).
作为 DCG,应该用短语/2 来调用它
?- phrase(splitter(" ", Strings), "Hello world !"), maplist(atom_codes,Atoms,Strings).
Strings = [[72, 101, 108, 108, 111], [119, 111, 114, 108, 100], [33]],
Atoms = ['Hello', world, !] .