What I’m trying to do is given a list of lists like the one bellow:
inicialState(e([5, 5, 5], [6, 5, 5])).
I want to calculate the some of all the elements of one list and compare it to a certain value, for that I want to analyze the list of lists above and analyze each list separately and do the calculations, for example I want to see if 5+5+5, of the first list, is equal to “N” and print “True” or “False”, then analyze the second list and see if 6+5+5 is equal to “N” and print the same thing.
The problem is the functions I have only print me a “no” in the console.
add([], 0).
add([H|T], Sum) :- add(T, Rest), Sum is H + Rest.
equal(_, []).
equal(Target, [H|T]) :- add(H, Sum), write(Sum), (Target =:= Sum -> write('True') ; write('False')), equal(Target, T).
I call the following function:
testLists(Size) :-
N is (Size + (Size*(Size*Size))) / 2,
write(N),
nl,
inicialState(List),
equal(N, List).
I tried doing the following function:
equal2(Target, Sum) :- nl, Target =:= Sum -> write('True') ; write('False').
The problem with this function is that it works, but only for one list, not a list of lists, and I have to call a separate function to add every element of the list like the one below:
add([], 0).
add([H|T], Sum) :- add(T, Rest), Sum is H + Rest.