序言:删除所有空格名单
我有这样的名单列表:序言:删除所有空格名单
[[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匹配[]作为空间。 我该如何做到这一点?
:- 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
由于@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
s(X)。奖金问题:我如何自动找出/测试(没有阅读所有文档)哪些内置/库谓词足够纯粹,以满足我的口味? – repeat
@repeat:我可能会尝试[tag:logical-purity]的标签信息中的两个特征。在这种情况下,如果一个参数被实例化,但是泛化,最一般的查询导致实例化错误,char_type/2(我假设这是你引用的内建函数)成功。然而,我使用它的方式第二个参数总是“空间”,并且以这种方式使用它不会破坏连接的交换性,例如, '? - X ='',char_type(X,空格).'和'? - char_type(X,空格),X =''。。产生相同的结果。在这种情况下,这对我来说已经足够了。 – tas
Thx 4 Ur回复!对我来说听起来很好......只是想知道如何自动检查这一点,以帮助程序员/编码员/用户/我自己看到纯粹与不纯之间的界限。 – repeat
代码:
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]] ?
为什么不委派 “递归部分”,以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
你怎么代表空间?你需要写''''! – false