SWI Prolog:差异列表 - fifo
我正在学习 Prolog,差异列表对我来说非常新,看起来就像来自地狱,尤其是我绝对是一个可怜的、头脑简单的 C++ 人:) 我的任务有问题:
addall(-E, +G, +S, -R) 添加所有结果 替换变量 E 满足 目标 G(变量 E 出现在其中) 集合 S 并返回新集合 R (此谓词类似于标准谓词 findall=3 和 findall=4)。
我已经使用 LIFO 集合实现了这一点,没有任何问题,但 FIFO 有问题,请考虑以下用法:
lifo_empty(L), lifo_addall(X,number(odd,X), L, W).
可以,集合 W 是 [8,6,4,2,0] 但是:
fifo_empty(Q), fifo_addall(X, podstaw(c,X), Q, W).
给我 W = [9, 7, 5, 3|[1|_G3761]-_G3761]
。我想要的是[9,7,5,3,1|_G3761]-_G3761
。我不知道出了什么问题,尤其是:
?- fifo_empty(Q), fifo_put(a,Q,W), fifo_put(b,W,C).
C = [a, b|_G3922]-_G3922.
工作得很好。我的代码:
fifo_empty(X-X).
todiff(X, [X|Xs]-Xs).
fifo_put(E, X-[E|Xs], X-Xs).
fifo_get([E|X]-Xs, X-Xs, E).
fifo_append([],S,S).
fifo_append([H|T],S,W) :- fifo_put(H,S,NS), lifo_append(T,NS,W).
fifo_addall(E, Goal, S, R) :- findall(E,Goal, W), fifo_append(W,S,R).
提前致谢。
I am learning Prolog and difference lists are super new to me and looks like from hell, especially that I am definitely poor simple-minded C++ guy :)
I have problem with my task:
addall(-E, +G, +S, -R) adds all results of
substitutions for the variable E which satisfy
the goal G (in which the variable E occurs) to
a collection S and returns a new collection R
(this predicate resembles the standard predicates findall=3 and findall=4).
I have implemented this with LIFO collection with no problem but something is wrong with FIFO, consider following usage:
lifo_empty(L), lifo_addall(X,number(odd,X), L, W).
is ok, collection W is [8,6,4,2,0] but:
fifo_empty(Q), fifo_addall(X, podstaw(c,X), Q, W).
gives me W = [9, 7, 5, 3|[1|_G3761]-_G3761]
. What I want is [9,7,5,3,1|_G3761]-_G3761
. I have no idea what is wrong, especially that:
?- fifo_empty(Q), fifo_put(a,Q,W), fifo_put(b,W,C).
C = [a, b|_G3922]-_G3922.
works just fine. My code:
fifo_empty(X-X).
todiff(X, [X|Xs]-Xs).
fifo_put(E, X-[E|Xs], X-Xs).
fifo_get([E|X]-Xs, X-Xs, E).
fifo_append([],S,S).
fifo_append([H|T],S,W) :- fifo_put(H,S,NS), lifo_append(T,NS,W).
fifo_addall(E, Goal, S, R) :- findall(E,Goal, W), fifo_append(W,S,R).
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是否是因为您调用
lifo_append/3
作为该行中的最后一个目标:...而不是
fifo_append/3
?尝试将最后一个子目标调用更改为fifo_append/3
。使用以下事实来测试此更改的代码:给出:
...其中
W
绑定到您所描述的模式。Could this be happening because you're calling
lifo_append/3
as the last goal in the line:...instead of
fifo_append/3
? Try changing that last subgoal call tofifo_append/3
. Testing your code with this change with the following facts:Gives:
...where
W
binds to the pattern you described you were after.