简体   繁体   中英

Prolog - Creating a list by iterating facts

I am new in Prolog and wanted to learn it. I have been trying to create a list while iterating facts but list that was created one step back refreshing and lost the data. How can I keep the data and add elements to list?

funct(a,b,1).
funct(b,c,2).
funct(b,d,3).

creatingList(X,Y,R):- funct(X,Y,A), funct(Y,Z,B), \+member(B,R) , append(R,B,R).

I tried that , but when I trace it keeps losing data, which I expect is R = [1,2,3].

@User is correct:

?- findall(funct(X,Y,Z), funct(X,Y,Z), Functs).
Functs = [funct(a, b, 1), funct(b, c, 2), funct(b, d, 3)].

setof/3 and bagof/3 might also apply, but their semantics are a little more complex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM