2016-04-24 70 views
3

我有這樣的名單列表:序言:刪除所有空格名單

[[q, ,w, ,e, ,r, ,t, ,z],[a, ,s, ,d, ,f, ,g, ,h],[y, ,x, ,c, ,v, ,b, ,n]] 

,我需要刪除除去年列表中的所有空間。 所以我想:

[[q,w,e,r,t,z],[a,s,d,f,g,h],[y, ,x, ,c, ,v, ,b, ,n]] 

我想:

deleteAll([_|[]],[]). 
deleteAll([Head|Tail],L) :- 
    deleteAll(Tail,_), 
    subtract(Head,[ ],L). 

但它不工作。我越來越onlny:

[q, ,w, ,e, ,r, ,t, ,z] 

如此看來,即使 didnt匹配[]作爲空間。 我該如何做到這一點?

+5

你怎麼代表空間?你需要寫''''! – false

回答

3
 
:- set_prolog_flag(double_quotes, chars). 
:- use_module(library(double_quotes)). 

spdels([], []). 
spdels([Cs], [Cs]). 
spdels([Cs|Css], [Ds|Dss]) :- 
    Css = [_|_], 
    Dss = [_|_], 
    text_nospaces(Cs, Ds), 
    spdels(Css, Dss). 

text_nospaces([], []). 
text_nospaces([C|Cs], Ds0) :- 
    if_(C = ' ', Ds0 = Ds1, Ds0 = [C|Ds1]), 
    text_nospaces(Cs, Ds1). 


text_nospaces_bis(Cs, Ds) :- 
    tfilter(dif(' '), Cs, Ds). 

使用和tfilter/3

| ?- spdels(["a b c","d e","f g"], Cs). 
Cs = ["abc","de","f g"] ? ; 
no 
2

由於@false已經指出[ ]不是空格而是空列表。另外,您的謂詞將L描述爲Head減去空列表,並且它不關心遞歸的結果(deleteAll(Tail,_))。這就是爲什麼你得到未改變的第一個列表作爲結果。

想想謂語應說明:,列出兩個表之間的關係,其中第二列表包含不受空間的第一個列表的子列表除了最後一個子列表,這是不變的:

:- set_prolog_flag(double_quotes, chars). 

lists_withoutspace([X],[X]).     % last list unaltered 
lists_withoutspace([H1,H2|T1],[H1WoS|T2]) :- % H1Wos: 
    list_withoutspace(H1,H1WoS),     % first sublist without spaces 
    lists_withoutspace([H2|T1],T2).    % the same for the rests 

如果你想匹配多個字母改變alpha相應

list_withoutspace([],[]).   % empty list contains no space 
list_withoutspace([X|T],L) :-  % X is not in the list 
    char_type(X,space),    % if it is space 
    list_withoutspace(T,L).   % relation must also hold for tail 
list_withoutspace([X|T],[X|L]) :- % X is in the list 
    char_type(X,alpha),    % if it is a letter 
    list_withoutspace(T,L).   % relation must also hold for tail 

:對於list_withoutspace/2,你可以使用TE內部謂詞char_type/2來確定第一列表元素的類型。如果您查詢此斷言,你得到期望的結果:

?- lists_withoutspace([[q,' ',w,' ',e,' ',r,' ',t,' ',z],[a,' ',s,' ',d,' ',f,' ',g,' ',h],[y,' ',x,' ',c,' ',v,' ',b,' ',n]],L). 
L = [[q,w,e,r,t,z],[a,s,d,f,g,h],[y,' ',x,' ',c,' ',v,' ',b,' ',n]] ? ; 
no 

或者更簡潔:

?- lists_withoutspace(["q w e r t z","a s d f g h","y x c v b n"],L). 
L = [[q,w,e,r,t,z],[a,s,d,f,g,h],[y,' ',x,' ',c,' ',v,' ',b,' ',n]] ? ; 
no 
+2

s(X)。獎金問題:我如何自動找出/測試(沒有閱讀所有文檔)哪些內置/庫謂詞足夠純粹,以滿足我的口味? – repeat

+2

@repeat:我可能會嘗試[tag:logical-purity]的標籤信息中的兩個特徵。在這種情況下,如果一個參數被實例化,但是泛化,最一般的查詢導致實例化錯誤,char_type/2(我假設這是你引用的內建函數)成功。然而,我使用它的方式第二個參數總是「空間」,並且以這種方式使用它不會破壞連接的交換性,例如, '? - X ='',char_type(X,空格).'和'? - char_type(X,空格),X =''。。產生相同的結果。在這種情況下,這對我來說已經足夠了。 – tas

+1

Thx 4 Ur回覆!對我來說聽起來很好......只是想知道如何自動檢查這一點,以幫助程序員/編碼員/用戶/我自己看到純粹與不純之間的界限。 – repeat

0

代碼:

deleteAllSpaces_except_last([X],[X]):-!.    % without last Element 

deleteAllSpaces_except_last([[]|Ys],[[]|Ys1]):-   % End of List inside List_of_lists 
     deleteAllSpaces_except_last(Ys,Ys1). 

deleteAllSpaces_except_last([[X|Xs]|Ys],Res):-   % if X=' ' then skip else add into New list inside list_of_lists 
     (X=' ',Res=[Xs1|Ys1];Res=[[X|Xs1]|Ys1]), 
     deleteAllSpaces_except_last([Xs|Ys],[Xs1|Ys1]). 

測試:

| ?- deleteAllSpaces_except_last([[q,' ',w,' ',e,' ',r,' ',t,' ',z],[a,' ',s,' ',d,' ',f,' ',g,' ',h],[y,' ',x,' ',c,' ',v,' ',b,' ',n]],L). 
L = [[q,w,e,r,t,z],[a,s,d,f,g,h],[y,' ',x,' ',c,' ',v,' ',b,' ',n]] ? ; 
no 

| ?- deleteAllSpaces_except_last([[q,' ',w,' ',e,' ',r,' ',t,' '],[],[y,' ',x,' ',c,' ',v,' ',b,' ',n]],L). 
L = [[q,w,e,r,t],[],[y,' ',x,' ',c,' ',v,' ',b,' ',n]] ? 
+1

'deleteAllSpaces_except_last([[C],[]],Xs).'應該給出兩個答案 – false

+0

@false我加了'deleteAllSpaces_except_last([[]],[])。'? –

+1

'X = a,deleteAllSpaces_except_last([[X],[]],[[X],[]])。'正確成功,但沒有'X = a'時失敗。 – false

3

爲什麼不委派 「遞歸部分」,以Prolog的圖書館謂詞?基於tfilter/3

dif/3定義spaces_gone/2像這樣:使用SICStus序言4.3.2

 
:- use_module(library(lists), [same_length/2, reverse/2, maplist/3]). 

spaces_gone([], []). 
spaces_gone([A|As], [D|Ds]) :- 
    same_length(As, Ds), 
    reverse([A|As], [Last|Bs]), 
    maplist(tfilter(dif(' ')), Bs, Cs), 
    reverse([Last|Cs], [D|Ds]). 

樣品查詢:

| ?- set_prolog_flag(double_quotes, chars), 
    use_module(library(double_quotes)). 
% ... 
yes 

| ?- spaces_gone(["a b c","d e","f g"], Css). 
Css = ["abc","de","f g"] ? ; 
no