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.
Could this be happening because you're calling lifo_append/3
as the last goal in the line:
fifo_append([H|T],S,W) :- fifo_put(H,S,NS), lifo_append(T,NS,W).
...instead of fifo_append/3
? Try changing that last subgoal call to fifo_append/3
. Testing your code with this change with the following facts:
person(joe).
person(mary).
Gives:
?- fifo_empty(Q), fifo_addall(X, person(X), Q, W).
Q = [joe, mary|_G677]-[joe, mary|_G677],
W = [joe, mary|_G677]-_G677.
...where W
binds to the pattern you described you were after.
精彩评论