Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does foreach only loop once?

Tags:

prolog

The following code runs in 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]
like image 804
z_axis Avatar asked Jul 24 '26 09:07

z_axis


1 Answers

foreach/2 is more complex that forall, and you hit the case where the used variable quantification makes a difference. Try instead

?- forall(member(X, ["1","2","3"]), (number_codes(Y,X), writeln(Y))).
1
2
3

true.

It seems that foreach/2 retains the bindings for Y.

like image 174
CapelliC Avatar answered Jul 26 '26 10:07

CapelliC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!