gpt4 book ai didi

prolog - 在序言中嵌套 for 循环

转载 作者:行者123 更新时间:2023-12-05 09:26:06 27 4
gpt4 key购买 nike

我对 Prolog 很陌生,所以我不确定如何做到这一点。

我正在尝试创建一个嵌套的 for 循环,在每一行输出三个变量。

我正在尝试获得如下循环的输出:

SWI-序言

?- out(A, B, C).
A = 1, B = 1, C = 1 (Next)
A = 1, B = 1, C = 2 (Next)
A = 1, B = 2, C = 1 (Next)

A = 1, B = 2, C = 2 (Next)
A = 2, B = 1, C = 1 (Next)
A = 2, B = 1, C = 2 (Next)

A = 2, B = 2, C = 1 (Next)
A = 2, B = 2, C = 2 (Next)

它需要有一个自定义范围

在 Python 中它看起来像这样

for i in range(1,3):
for j in range(1,3):
for k in range(1,3):
print(i, j, k)

最佳答案

out(A, B, C) :-
maplist(between(1, 2), [A, B, C]).

结果:

?- out(A, B, C).
A = B, B = C, C = 1 ;
A = B, B = 1,
C = 2 ;
A = C, C = 1,
B = 2 ;
A = 1,
B = C, C = 2 ;
A = 2,
B = C, C = 1 ;
A = C, C = 2,
B = 1 ;
A = B, B = 2,
C = 1 ;
A = B, B = C, C = 2.

关于prolog - 在序言中嵌套 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74467410/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com