zero sum game
#2549
Replies: 1 comment 1 reply
-
You can limit the length of the list before. This helps with non-termination at least: % In this case the sum can have at most 4 digits.
?- N in 3..4, label([N]), length(Sum, N), ls1_ls2_sum([2,4,3],[5,6,4], Sum).
N = 3, Sum = [7,0,8]
; N = 4, Sum = [7,0,8,0]. % Still need to prune this out. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The following code works, although I'm not convinced it's the most elegant way to approach the problem. However, as you can see in the output, there's a bigger problem -- the output continually appends zeros, making the answer incorrect, unless I use
once/1
. I have tried various methods to fix this, but they end up resulting in nontermination.For example, if I modify the main predicate like so:
I have tried using
if_
in various ways, but the result is the same.The only methods I have found so far that work are
once/1
and!/0
, (which are 👎 /0 if you know what I'm sayin').I have a feeling there's some really fundamental logical principles going on here that I'm just overlooking, but my brain isn't seeing it.
Edit: I have also tried using failure-slices and
debug:$/1
-- I've gathered enough to realize that the constraint solver is now searching for a new answer without a trailing zero, but this is impossible, which is why it will never terminate. What I can't figure out is how to make Prolog or clpz aware that it is impossible there could be any more meaningful answers and thus all choice points should be exhausted.Beta Was this translation helpful? Give feedback.
All reactions