gpt4 book ai didi

序言 - 为什么我应该使用 'dif' 而不是 '=\='

转载 作者:行者123 更新时间:2023-12-04 10:22:20 25 4
gpt4 key购买 nike

我正在学习“Prolog by Example”一书中的一个例子(Helder Coelho 和 Jose C Cotta)。

Write a program for designing an architectural unit
obeying the following specifications:

+ Two rectangular rooms
+ Each room has a window and interior door
+ Rooms are connected by interior door
+ One room also has an exterior door
+ A wall can have only one door or window
+ No window can face north
+ Windows cannot be on opposite sides of the unit

enter image description here

有一个解决方案,但它在 SWI-Prolog 中不起作用,甚至无法编译。

我不得不从书中更改解决方案:使用 'dif(X,Y)' 而不是 'X =\= Y'。

有人可以解释一下:为什么原始解决方案不起作用?

这是一个代码:
plan(FD,D1,W1,D2,W2) :-
frontroom(FD,D1,W1),
opposite(D1,D2),
room(D2,W2),
notopposite(W1,W2).

frontroom(FD,D,W) :-
room(D,W),
direction(FD),
dif(FD,D), % FD =\= D, % <- original was commented
dif(FD,W). % FD =\= W. % <- original was commented

room(D,W) :-
direction(D),
direction(W),
dif(D,W), % D =\= W, % <- original was commented
dif(W,north). % W =\= north. % <- original was commented

direction(north).
direction(south).
direction(east).
direction(west).

opposite(north,south).
opposite(south,north).
opposite(east,west).
opposite(west,east).

notopposite(D1,D2) :-
opposite(D1,D3),
dif(D2,D3). % D2 =\= D3. % <- original was commented

我可以查询并得到答案:
?- plan(west,D1,W1,D2,W2).
D1 = east,
W1 = south,
D2 = west,
W2 = south

我正在尝试跟踪原始解决方案(使用 D2 =\= D3 而不是 dif(D2,D3))并得到这个:
[trace]  ?- plan(west,D1,W1,D2,W2).
Call: (8) plan(west, _9632, _9634, _9636, _9638) ? creep
Call: (9) frontroom(west, _9632, _9634) ? creep
Call: (10) room(_9632, _9634) ? creep
Call: (11) direction(_9632) ? creep
Exit: (11) direction(north) ? creep
Call: (11) direction(_9634) ? creep
Exit: (11) direction(north) ? creep
Call: (11) north=\=north ? creep
ERROR: Arithmetic: `north/0' is not a function

为什么我不能使用'=\='?

最佳答案

应该是\==比不正确的 =\= .我不知道 =\=除了算术不等式之外,还有别的意思。

1982年第3版为online=\=
even documented在 1978 年作为 DECsystem 10 Prolog 一部分的同一文档中。因此,即使在上述书籍的此先前版本中也存在矛盾。

正文到此为止。但你说得对dif/2优于 (\==)/2 .见 this更多。

关于序言 - 为什么我应该使用 'dif' 而不是 '=\=',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60800828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com