I am trying to find a value score
, which can be calculated imperatively from two equal length lists Xs, Ys
as:
for i in len(Xs):
if Xs[i] == Ys[i]:
score++
else:
score--
So the idea is to basically check every element from left to right on both lists and increment score if those elements are the same, decrement score otherwise. For example:
score([1,2,3], [1,2,3]) == 3
score([1,2,3], [1,2,1]) == 1
score([1,2,3], [3,2,1]) == -1
score([1,2,3], [3,1,2]) == -3
I think I actually managed to write the program as:
score(Xs, Ys, C) ?=>
Xs = [], Ys = [], C = 0.
score(Xs, Ys, C) =>
Xs = [XH|XT],
Ys = [YH|YT],
if (XH #= YH) then
C #= C1 + 1
else
C #= C1 - 1
end,
score(XT, YT, C1).
the code gives the correct results when I query the score:
score([1,2,3], [1,2,3], S).
S = 3 ?;
score([1,2,3], [1,2,1], S).
S = 1 ?;
score([1,2,3], [3,2,1], S).
S = -1 ?;
score([1,2,3], [3,1,2], S).
S = -3 ?;
However, when I try to generate the lists which produce the scores, the code only generates the identical list with score 3:
S :: -3..3, Ls = new_list(3), Ls :: 1..3, score([1,2,3], Ls, S), solve([Ls, S]).
S = 3
Ls = [1,2,3] ?;
no
I want to generate all the lists, corresponding to all the possible scores, I have a feeling that I have to modify the base case but I can't figure out how :)
EDIT: I also tried to do a tail-recursion type of solution, it again gives correct results when the scores are queried, but it can only generate the identical list.
score(Xs, Ys, C) ?=>
score_helper(Xs, Ys, 0, C).
score_helper([], _, Cur, C) ?=>
C = Cur.
score_helper(Xs, Ys, Cur, C) ?=>
Xs = [XH|XT],
Ys = [YH|YT],
if (XH = YH) then
score_helper(XT, YT, Cur+1, C)
else
score_helper(XT, YT, Cur-1, C)
end.