计算 Prolog 中的定子句语法递归
我有以下 Prolog 定子句语法:
s-->[a],s,[b].
s-->[].
这将导致像 [a,a,b,b] 这样的单词与 [a,b,a,b] 这样的单词相反被接受。简而言之,语法显然是a^nb^n。现在我想将 n 返回给用户。我如何计算n?
I have the following Prolog definite clause grammar:
s-->[a],s,[b].
s-->[].
This will result in words like [a,a,b,b] being accepted in opposite to words like [a,b,a,b]. To put it in a nutshell the grammar is obviously a^n b^n. Now I want to return n to the user. How can I calculate n?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
需要给DCG非终端提供参数。请注意,相等并不像命令式编程语言的赋值那样工作,因此使用了 X is Y + 1。
一些示例输出:
One needs to give parameters to the DCG non terminals. Please take care equality doesn't work exactly like assignment of imperative programming languages so X is Y + 1 was used.
Some sample outputs:
用法:
Usage:
@thequark 和 的答案@LittleBobbyTables 与地面字符串一起使用时效果很好。
但是,如果它们的长度不受限制,就像下面的查询一样,该怎么办?
我们当然希望像上面这样的查询普遍终止!
让我们使用 clpfd 并编写:
示例查询:
The answers by @thequark and by @LittleBobbyTables work fine when used with ground strings.
But what if they are not bounded in length, like in the following queries?
We surely want queries like the one above to terminate universally!
Let's use clpfd and write:
Sample queries: