Prolog算术语法

发布于 2024-08-16 04:13:37 字数 131 浏览 5 评论 0原文

如何将a定义为整数/浮点数?

我想找到 a+b+c+d=10 的结果,其中 a,b,c,d 是整数且 >=0代码>.

How to define a as a integer/float number ?

I want to find the results of a+b+c+d=10 where a,b,c,d is integer and >=0.

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

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

发布评论

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

评论(3

毁梦 2024-08-23 04:13:37

这是一个简单、现代、纯 Prolog、非 CLP 库的解决方案:

range(X):-
        member(X,[0,1,2,3,4,5,6,7,8,9,10]).

ten(A,B,C,D):-
        range(A),
        range(B),
        range(C),
        range(D),
        10 =:= A + B + C + D.

Here is a simple, modern, pure Prolog, non-CLP-library solution:

range(X):-
        member(X,[0,1,2,3,4,5,6,7,8,9,10]).

ten(A,B,C,D):-
        range(A),
        range(B),
        range(C),
        range(D),
        10 =:= A + B + C + D.
夜未央樱花落 2024-08-23 04:13:37

通过 SWI-Prolog,您可以使用 CLP(FD) 库

1 ?- use_module(library(clpfd)).
%  library(error) compiled into error 0.00 sec, 9,764 bytes
% library(clpfd) compiled into clpfd 0.05 sec, 227,496 bytes
true.

2 ?- Vars=[A,B,C,D],Vars ins 0..10,sum(Vars,#=,10),label(Vars).
Vars = [0, 0, 0, 10],
A = 0,
B = 0,
C = 0,
D = 10 ;
Vars = [0, 0, 1, 9],
A = 0,
B = 0,
C = 1,
D = 9 ;
Vars = [0, 0, 2, 8],
A = 0,
B = 0,
C = 2,
D = 8 ;
Vars = [0, 0, 3, 7],
A = 0,
B = 0,
C = 3,
D = 7 ;
...

with SWI-Prolog you can use CLP(FD) library

1 ?- use_module(library(clpfd)).
%  library(error) compiled into error 0.00 sec, 9,764 bytes
% library(clpfd) compiled into clpfd 0.05 sec, 227,496 bytes
true.

2 ?- Vars=[A,B,C,D],Vars ins 0..10,sum(Vars,#=,10),label(Vars).
Vars = [0, 0, 0, 10],
A = 0,
B = 0,
C = 0,
D = 10 ;
Vars = [0, 0, 1, 9],
A = 0,
B = 0,
C = 1,
D = 9 ;
Vars = [0, 0, 2, 8],
A = 0,
B = 0,
C = 2,
D = 8 ;
Vars = [0, 0, 3, 7],
A = 0,
B = 0,
C = 3,
D = 7 ;
...
一紙繁鸢 2024-08-23 04:13:37

这是 GNU-Prolog 一段在有限域上进行约束求解的代码:

$ gprolog
| ?- [user].
compiling user for byte code...
ten(A,B,C,D) :- fd_domain([A,B,C,D],0,9999999), 10 #= A + B + C + D.

Ctrl + D

| ?- ten(A,B,C,D), fd_labeling([A,B,C,D]).

如您所见,它解决了大范围的问题,例如

A = 0
B = 0
C = 0
D = 10 ? ;

A = 0
B = 0
C = 1
D = 9 ? ;

A = 0
B = 0
C = 2
D = 8 ? ;
...

0-9999999感谢 Przemysław Kobylański 的博客,其中提供了清晰、非常好的 Prolog 示例,我在其中找到了鼓舞人心的示例。

PPS 在处理有限域时,您可能喜欢使用 fd_set_vector_max/ 1..在上面的情况下,它是不需要的,但取决于约束可能是有用的 - 当 Gnu-Prolog 在范围上操作时,当在可能值的向量上操作时,更多细节, 可以在手册“有限域求解器和内置谓词 - 简介”中找到

Here is GNU-Prolog piece of code with constraint solving over finite domains :

$ gprolog
| ?- [user].
compiling user for byte code...
ten(A,B,C,D) :- fd_domain([A,B,C,D],0,9999999), 10 #= A + B + C + D.

Ctrl + D

| ?- ten(A,B,C,D), fd_labeling([A,B,C,D]).

As you can see, it solves problem of big ranges like 0-9999999

A = 0
B = 0
C = 0
D = 10 ? ;

A = 0
B = 0
C = 1
D = 9 ? ;

A = 0
B = 0
C = 2
D = 8 ? ;
...

P.S. Thanks for Przemysław Kobylański for his blog with clear, very nice Prolog examples, where I've found inspiring examples.

P.P.S. When playing with finite domains, you might like to use fd_set_vector_max/1 . In above case it's not needed, but depending on constraint might be usefull - more details when Gnu-Prolog operates on ranges, when on vectors of possible values, can be found at manual "Finite domain solver and built-in predicates - Introduction"

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