以下代码在 SWI-Prolog 中运行:
?-foreach(member(X ,["1","2","3"]) ,(number_codes(Y,X),writeln(Y))).
1
false.
?- foreach(member(X ,["1","2","3"]) ,writeln(X)).
[49]
[50]
[51]
以下代码在 SWI-Prolog 中运行:
?-foreach(member(X ,["1","2","3"]) ,(number_codes(Y,X),writeln(Y))).
1
false.
?- foreach(member(X ,["1","2","3"]) ,writeln(X)).
[49]
[50]
[51]
foreach /2 比forall更复杂,并且您遇到了使用的变量量化产生影响的情况。试试吧
?- forall(member(X, ["1","2","3"]), (number_codes(Y,X), writeln(Y))).
1
2
3
真的。
似乎 foreach/2 保留了Y
.