# Add elements dynamically to an initially empty list recursively

**URL:** <https://swi-prolog.discourse.group/t/add-elements-dynamically-to-an-initially-empty-list-recursively/2802>\
**Category:** Help!\
**Created:** [August 19, 2020, 7:58am UTC](https://swi-prolog.discourse.group/t/add-elements-dynamically-to-an-initially-empty-list-recursively/2802 "2020-08-19T07:58:28Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [August 19, 2020, 8:58am UTC](https://swi-prolog.discourse.group/t/add-elements-dynamically-to-an-initially-empty-list-recursively/2802/4 "2020-08-19T08:58:34Z")

</div>

Since I don’t know how to construct the particular `P` and `R` for your code, here is something that I hope is similar enough to your need that will help.

```prolog
sum_list([H|T],R) :-
    sum_list(T,R0),
    plus(H,R0,R).
sum_list([],0).

```

Examples

```prolog
?- sum_list([],R).
R = 0.

?- sum_list([1],R).
R = 1.

?- sum_list([1,2],R).
R = 3.

?- call(sum_list,[],R).
R = 0.

?- call(sum_list,[1],R).
R = 1.

?- call(sum_list,[1,2],R).
R = 3.

```

* * *

Based on the predicate name `doCallProve` this looks like you could be trying to do proofs with Prolog. In the that case you might want to switch from the default reasoning of [backward chaining](https://en.wikipedia.org/wiki/Backward_chaining) in Prolog to [forward chaining](https://en.wikipedia.org/wiki/Forward_chaining).

See:  
[Pfc](https://www.swi-prolog.org/pack/list?p=pfc) – a package for forward chaining in Prolog  
[Pfc pack (forward chaining) – can’t get to work](https://swi-prolog.discourse.group/t/pfc-pack-forward-chaining-cant-get-to-work/2662)

HTH

---

_[View the full topic](https://swi-prolog.discourse.group/t/add-elements-dynamically-to-an-initially-empty-list-recursively/2802)._
