findall/3自动地寻找目标,并把结果储存到一个列表中。使用它可以方便的把stuff子句还原成列表。 ?- findall(X, stuff(X), L). L = [pencil, cookie, snow] 下面把所有与厨房相连的房间找出来。 ?- findall(X, connect(kitchen, X), L). L = [office, cellar, ‘dining room’] 最后我们再来看一...
e.g. factorial(number, 1, answer) -> answer := number! factorial(1, F, F). factorial(N, T, F) :- N > 1, NEXT_T is N * T, NEXT_N is N - 1, factorial(NEXT_N, NEXT_T, F). 5! = 120;15. Natural LanguageAnd Proof: first find out all the possible breakdown of a ...
factorial_1(N,F):-N > 1,NN is N - 1,factorial_1(NN,FF),F is N FF. ?- factorial_1(5,X). X = 120 如果引入一个新的变量来储存前面调用的结果,我们就可以把 factorial/3 写成尾递归的形式。新的参数的初始值为 1 。每次递归调用将计算第二个参数的值,当到达了边界条件,第三个参数就绑定...
we had to look up Prolog versions of the Fibonacci sequence and the calculation of factorial numbers. However, in an effort to really dig my teeth into this language, I wanted to see if I could solve these problems without resorting