且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将函数的输出转换为列表?

更新时间:2022-04-25 00:49:54

但是我需要使用该函数来制作另一个函数,以将前一个函数的输出转换为一个列表.作为Prolog的完整入门者,我不知道如何执行此操作.

But I need to use that function to make another function that turns the outputs of the former function into a list. Being a complete beginner with Prolog, I have no clue how to do this.

findall(+Template, :Goal, -Bag) : 创建实例化列表Template依次在Goal上回溯,并将结果与​​Bag统一.

findall(+Template, :Goal, -Bag): Creates a list of the instantiations Template gets successively on backtracking over Goal and unifies the result with Bag.

例如,如何收集从1到15的所有奇数:

For example, how to collect all odd numbers from 1 to 15:

odd( X ) :-
    X rem 2 =:= 1.

我们可以一一获得所有赔率.

We can get all that odds one-by-one.

?- between( 1, 15, X ), odd( X ).
X = 1 ;
X = 3 ;
X = 5 ;
X = 7 ;
X = 9 ;
X = 11 ;
X = 13 ;
X = 15.

我们可以将它们收集到一个列表中:

And we can collect them into a list:

?- findall(X, (between( 1, 15, X ), odd( X )), List).
List = [1, 3, 5, 7, 9, 11, 13, 15].