计算 Prolog 中的定子句语法递归

发布于 2024-09-10 14:07:42 字数 171 浏览 3 评论 0原文

我有以下 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 技术交流群。

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

发布评论

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

评论(3

苍暮颜 2024-09-17 14:07:42
s(X)-->[a],s(Y),[b],{X is Y+1}.
s(0)-->[].

需要给DCG非终端提供参数。请注意,相等并不像命令式编程语言的赋值那样工作,因此使用了 X is Y + 1。

一些示例输出:

s(X,[a,b,b,b],[]).
false.

s(X,[a,a,a,b,b,b],[]).
X = 3 ;
false.
s(X)-->[a],s(Y),[b],{X is Y+1}.
s(0)-->[].

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:

s(X,[a,b,b,b],[]).
false.

s(X,[a,a,a,b,b,b],[]).
X = 3 ;
false.
止于盛夏 2024-09-17 14:07:42
s(N, M) --> [a], {N1 is N + 1}, s(N1, M), [b].
s(N, N) --> [].
s(N) --> s(0, N).

用法:

?- phrase(s(N), [a,a,a,b,b,b]).
N = 3
s(N, M) --> [a], {N1 is N + 1}, s(N1, M), [b].
s(N, N) --> [].
s(N) --> s(0, N).

Usage:

?- phrase(s(N), [a,a,a,b,b,b]).
N = 3
撩起发的微风 2024-09-17 14:07:42

@thequark的答案@LittleBobbyTables 与地面字符串一起使用时效果很好。

但是,如果它们的长度不受限制,就像下面的查询一样,该怎么办?

?- phrase(s(3),_).               % expected: success   
%                                  observed: no answer(s)

?- phrase(s(-10),_).             % expected: failure
%                                  observed: no answer(s)

我们当然希望像上面这样的查询普遍终止!
让我们使用 并编写:

:- use_module(library(clpfd)).

s(0) --> [].
s(N) --> {N#>0,N#=N0+1},[a],s(N0),[b].

示例查询:

?- phrase(s(N),[a,a,a,a,b,b,b,b]).
N = 4 ;                          % works like in the other answers
false.

?- phrase(s(3),Xs).
Xs = [a,a,a,b,b,b] ;             % now, this works too!
false.                           % (terminates universally)

?- phrase(s(-10),_).             % fails, like it should
false.

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?

?- phrase(s(3),_).               % expected: success   
%                                  observed: no answer(s)

?- phrase(s(-10),_).             % expected: failure
%                                  observed: no answer(s)

We surely want queries like the one above to terminate universally!
Let's use and write:

:- use_module(library(clpfd)).

s(0) --> [].
s(N) --> {N#>0,N#=N0+1},[a],s(N0),[b].

Sample queries:

?- phrase(s(N),[a,a,a,a,b,b,b,b]).
N = 4 ;                          % works like in the other answers
false.

?- phrase(s(3),Xs).
Xs = [a,a,a,b,b,b] ;             % now, this works too!
false.                           % (terminates universally)

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