Foreach bug?

First of all, I cannot reproduce @jstranik’s problem. Both work for me, giving the same result. Which version are you using?

@swi’s maplist/2 solution is a lot simpler and more robust. I generally have my doubt about foreach/2. It is unclear what needs to be copied. The original implementation had both semantic and performance issues. The current one has different semantic issues as pointed out in this thread :frowning:

Possibly we should deprecate it? Or, could we prove it is sound and safe using an intermediate predicate that we can generate on the fly or using goal_expansion/2?