I assume you mean without explicit recursion? If so, you can use the implementation of the high-order predicate list fold left along with the lambda expression to avoid the need for an auxiliary predicate. Using Logtalk as an example, you can write:
?- Sum0 is 0, meta::fold_left([X,Y,Z]>>(Z is Y+X), Sum0, [1,2,3], Sum). Sum0 = 0, Sum = 6.
Logtalk can use most Prolog implementations ( http://logtalk.org/ ) as the main compiler. You can also use the Ulrich lambda library ( http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord.html ) with the supported Prolog compiler along with the Prolog library providing a folded left predicate for the same result. Using now YAP as an example:
$ yap ... ?- use_module(library(lambda)). ... ?- use_module(library(maplist)). ... ?- Sum0 is 0, foldl(\X^Y^Z^(Z is Y+X), [1,2,3], Sum0, Sum). Sum = 6, Sum0 = 0.
In short, the folded left predicate iterates over the list, recursively applying the closure in its first argument to the list item and battery, returning the final battery value.
source share