2013-07-01 87 views
3

我有这样的原子,例如:白空间/ 3

"The pen 
is on 
the table" 

如果我使用atomic_list_concat(L, ' ', S),我获得:

L =[The, pen 
    , is, on 
    , the, Table] 

而不是:

L =[The, pen, is, on, the, Table] 

为什么?我怎样才能获得第二个解决方案?

编辑:是否可能在atomic_list_concat(L, Separator, S)中使用两个分隔符?

回答

3

怪异输出的原因是因为文本中有换行符。而且多个空格会导致有趣的结果。

你可以添加一个'清理'程序。对不起它的必要性,复杂的原子/码表示法之间切换:

cleanup(Dirty, Clean) :- maplist(only_graphs, Dirty, Clean). 

only_graphs(Word, Clean) :- 
    atom_codes(Word, X), 
    include(keep_graph, X, Y), 
    atom_codes(Clean, Y). 

keep_graph(C) :- code_type(C, graph). 

编辑:根据您的SW组织,您可以重新定义 atomic_list_concat/3,但会使代码库脆。

我会建议,而不是用一些描述性的,像

separe_words(Atom, Words) :- 
    atomic_list_concat(Dirty, ' ', Atom), 
    maplist(only_graphs, Dirty, Words). 

,并添加到您的〜/ .plrc,或你的图书馆

编辑因为你发现,有normalize_space/2 ,那会更好。 用法应该是

separe_words(Atom, Words) :- 
     normalize_space(atom(Clean), Atom), 
     atomic_list_concat(Words, ' ', Clean). 
+0

在我的系统我用atomic_list_concat(输出,分离器,在),是不可能性使用两个分隔符,而不是只有一个?例如['','\ n'] –

+0

不,那没有任何意义。毕竟,这是一个串联。系统应该在多个分隔符中选择什么?如果您需要更多灵活性,请与DCG联系。 – CapelliC

+1

和normalize_space/2?我在库(xpath)中找到了这个谓词。 –