Prolog:删除字符流中的多余空格

发布于 2024-10-10 12:35:49 字数 914 浏览 4 评论 0原文

Prolog 完全是新手。这让我有点沮丧。下面我的“解决方案”是我尝试使 Prolog 程序化...

这将删除空格或在逗号后插入空格(如果需要),也就是说,直到遇到句点:

squish:-get0(C),put(C),rest(C).  
rest(46):-!.  
rest(32):-get(C),put(C),rest(C).  
rest(44):-put(32), get(C), put(C), rest(C).  
rest(Letter):-squish. 

目标:我想知道如何删除之前的任何空格逗号也是如此。

以下内容有效,但在很多层面上都是错误的,尤其是“退出”!

squish:-  
  get0(C),  
  get0(D),  
  iteratesquish(C,D).  

iteratesquish(C,D):-  
  squishing(C,D),  
  get0(E),  
  iteratesquish(D,E).  

squishing(46,X):-put(46),write('end.'),!,exit.  

squishing(32,32):-!.  
squishing(32,44):-!.  
squishing(32,X):-put(32),!.   

squishing(44,32):-put(44),!.  
squishing(44,44):-put(44), put(32),!.  
squishing(44,46):-put(44), put(32),!.  
squishing(44,X):-put(44), put(32),!.  

squishing(X,32):-put(X),!.  
squishing(X,44):-put(X),!.  
squishing(X,46):-put(X),!.  

squishing(X,Y):-put(X),!.  

Total newb to Prolog. This one is frustrating me a bit. My 'solution' below is me trying to make Prolog procedural...

This will remove spaces or insert a space after a comma if needed, that is, until a period is encountered:

squish:-get0(C),put(C),rest(C).  
rest(46):-!.  
rest(32):-get(C),put(C),rest(C).  
rest(44):-put(32), get(C), put(C), rest(C).  
rest(Letter):-squish. 

GOAL: I'm wondering how to remove any whitespace BEFORE the comma as well.

The following works, but it is so wrong on so many levels, especially the 'exit'!

squish:-  
  get0(C),  
  get0(D),  
  iteratesquish(C,D).  

iteratesquish(C,D):-  
  squishing(C,D),  
  get0(E),  
  iteratesquish(D,E).  

squishing(46,X):-put(46),write('end.'),!,exit.  

squishing(32,32):-!.  
squishing(32,44):-!.  
squishing(32,X):-put(32),!.   

squishing(44,32):-put(44),!.  
squishing(44,44):-put(44), put(32),!.  
squishing(44,46):-put(44), put(32),!.  
squishing(44,X):-put(44), put(32),!.  

squishing(X,32):-put(X),!.  
squishing(X,44):-put(X),!.  
squishing(X,46):-put(X),!.  

squishing(X,Y):-put(X),!.  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诠释孤独 2024-10-17 12:35:49

由于您正在描述列表(在本例中:字符代码),请考虑使用 DCG 表示法。例如,要让任何逗号后跟一个空格,请考虑使用类似于以下的代码:

squish([])                 --> [].
squish([(0',),(0' )|Rest]) --> [0',], spaces, !, squish(Rest).
squish([L|Ls])             --> [L], squish(Ls).

spaces --> [0' ], spaces.
spaces --> [].

示例查询:

?- phrase(squish(Ls), "a,   b,c"), format("~s", [Ls]).
a, b, c

因此,首先关注字符序列与所需的“干净”字符串之间关系的清晰声明性描述。然后,您可以使用 SWI-Prolog 的库(pio)通过这些语法规则读取文件。要删除逗号前面的所有空格,您只需向上面的 DCG 添加一条规则(以压缩//1),我将其作为练习留给您。当然,一个极端的情况是一个逗号后面跟着另一个逗号,在这种情况下,要求是矛盾的:-)

Since you are describing lists (in this case: of character codes), consider using DCG notation. For example, to let any comma be followed by a single whitespace, consider using code similar to:

squish([])                 --> [].
squish([(0',),(0' )|Rest]) --> [0',], spaces, !, squish(Rest).
squish([L|Ls])             --> [L], squish(Ls).

spaces --> [0' ], spaces.
spaces --> [].

Example query:

?- phrase(squish(Ls), "a,   b,c"), format("~s", [Ls]).
a, b, c

So, first focus on a clear declarative description of the relation between character sequences and the desired "clean" string. You can then use SWI-Prolog's library(pio) to read from files via these grammar rules. To remove all spaces preceding commas, you only have to add a single rule to the DCG above (to squish//1), which I leave as exercise to you. A corner case of course is if a comma is followed by another comma, in which case the requirements are contradictory :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文