1

I am trying to calculate the sum of values given. My attempt:

element(0, 988).
element(1, 5434).
element(2, 5433).
element(3, 4543).
element(4, 827).

addElements(5, 0).

addElements(INDEX, SUM):-     %sums from given index to the end of array
    element(INDEX, VALUE),
    addElements(INDEX+1, SUM-VALUE).

My query:

addElements(0,X).

This is not working. Is it a syntactic error?

1 Answer 1

1

To evaluate arithmetic expressions in Prolog, you need to use the ISO built-in predicate is/2:

?- Index = 1, NewIndex is Index + 1.
Index = 1,
NewIndex = 2.

?- Accumulator = 10, NewAccumulator is Accumulator + 5.
Accumulator = 10,
NewAccumulator = 15.

Thus, assuming the indices are consecutive integers, a possible solution is:

add_elements(Index, Sum) :-
    add_elements_loop(Index, 0, Sum).

add_elements_loop(Index, Accumulator, Sum) :-
    not(element(Index, _)),              % index out of range!
    Sum = Accumulator.

add_elements_loop(Index, Accumulator, Sum) :-
    element(Index, Value),
    NewIndex is Index + 1,
    NewAccumulator is Accumulator + Value,
    add_elements_loop(NewIndex, NewAccumulator, Sum).

element(0, 70).
element(1, 30).
element(2, 10).
element(3, 20).
element(4, 80).

Examples:

?- add_elements(0, S).
S = 210 ;
false.

?- add_elements(3, S).
S = 100 ;
false.

An improved version of this code, which avoids the redundant call of the predicate element/2 and eliminates the spurious choice point, is as follows:

add_elements(Index, Sum) :-
    add_elements_loop(Index, 0, Sum).

add_elements_loop(Index, Accumulator, Sum) :-
    (   element(Index, Value)                             % if
    ->  NewIndex is Index + 1,                            % then 
        NewAccumulator is Accumulator + Value,
        add_elements_loop(NewIndex, NewAccumulator, Sum)
    ;   Sum = Accumulator ).                              % else
?- add_elements(0, S).
S = 210.

?- add_elements(3, S).
S = 100.
Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution! Even after i use is/2 in my solution, it only works when I use an "accumulator" parameter. Why?
@yukelid In fact, it's possible to solve the problem without an accumulator. Please post a new question showing how you've used is/2 in your code and what error you've got. This way it will be easier to clarify your doubt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.