表中的 Prolog 点
我有一个给定的列表,表示二维列表 x。该表包含两个 1 的“点”,如下例所示:
xxxxxxxxxxxxxxxx
xx1111xxxx111xxx
xxx1111xxxx11xxx
x1111xxxxxx111xx
我只需将第二个点从 1 更改为 2,如下例所示:
xxxxxxxxxxxxxxxx
xx1111xxxx222xxx
xxx1111xxxx22xxx
x1111xxxxxx222xx
我需要一个名为 separate(L,M)< 的谓词/code> 将采用第一个列表 L 并生成第二个表 M
如果我们能够在不使用任何标准谓词(如“findall”等)的情况下解决这个问题,那就太好了......
I have a given list that represent a two dimensional list x. This table contains two "spots" of 1 as you can see in the example below:
xxxxxxxxxxxxxxxx
xx1111xxxx111xxx
xxx1111xxxx11xxx
x1111xxxxxx111xx
I need to change ONLY the second spot from 1 to 2 like the example below:
xxxxxxxxxxxxxxxx
xx1111xxxx222xxx
xxx1111xxxx22xxx
x1111xxxxxx222xx
I need a predicate called separate(L,M)
that will take the first list L and will produce the second table M
It would be excellent if we can solve this without using any standard predicate like 'findall' etc ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用定语从句语法 (DCG) 来实现有限状态转换器。尽管 DCG 在生产方面没有提供太多组合操作,但它们在识别方面的实现相当出色。
所以你想要识别的是两种不同类型的 1 序列。所以基本上我猜输入行在扩展巴科斯范式(EBNF)中看起来如下:
对于识别问题,您可以编写一个没有属性的DCG(接下来是Prolog文本,您可以放入文件中或直接通过? - [用户]):
以下是一些运行示例:
对于生产问题,您只需向 DCG 添加属性即可。使用
差异列表最简单:
以下是一些运行示例:
注意:0' 是字符代码的 Prolog 表示法。在 ascii 中,我们有 0'x = 120、0'1 = 49、0'2 = 50,这解释了结果。上面的代码应该可以在大多数 Prolog 系统上运行,因为它们支持 DCG。
再见
定语从句语法
http://en.wikipedia.org/wiki/Definite_clause_grammar
有限状态传感器
http://en.wikipedia.org/wiki/Finite_state_transducer
You could use Definite Clause Grammars (DCG) to implement a finite state transducer. Although DCGs don't deliver much compositional operations on the production side, they are quite good in implementing the recognition side.
So what you want to recognize is two different types of runs of 1's. So basically I guess an input line looks as follows in Extended Backus Naur Form (EBNF):
For the recognition problem you can write a DCG without attributes (what follows is a Prolog text, you can put in a file or directly consult it via ?- [user]):
Here are some example runs:
For the production problem you just can add attributes to the DCG. Using
a difference list works easiest:
Here are some example runs:
Note: 0' is the Prolog notation for a character code. In ascii we have 0'x = 120, 0'1 = 49, 0'2 = 50, which explains the result. The above should run on most Prolog systems since they support DCGs.
Bye
Definite Clause Grammar
http://en.wikipedia.org/wiki/Definite_clause_grammar
Finite State Transducer
http://en.wikipedia.org/wiki/Finite_state_transducer
我们可以应用临时音译(仅适用于“水平”列表):
We can apply ad hoc transliteration (just for 'horizontal' lists):
与@chac建议的解决方案类似,但更直接。
Similar to solution suggested by @chac, but more direct.