From: Mark Thom Date: Sat, 30 Nov 2019 21:22:59 +0000 (-0700) Subject: add sumlist/2 to lists.pl X-Git-Tag: v0.8.118~36^2~14 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=27b659c401f2b509ce1ef404087a4faabd4ca4d3;p=scryer-prolog.git add sumlist/2 to lists.pl --- diff --git a/src/prolog/lib/lists.pl b/src/prolog/lib/lists.pl index 4fc5de96..ae207403 100644 --- a/src/prolog/lib/lists.pl +++ b/src/prolog/lib/lists.pl @@ -1,7 +1,10 @@ :- module(lists, [member/2, select/3, append/3, memberchk/2, reverse/2, length/2, maplist/2, maplist/3, maplist/4, maplist/5, maplist/6, maplist/7, - maplist/8, maplist/9]). + maplist/8, maplist/9, sumlist/2]). + + +:- use_module(library(error)). length(Xs, N) :- @@ -96,3 +99,13 @@ maplist(_, [], [], [], [], [], [], [], []). maplist(Cont, [E1|E1s], [E2|E2s], [E3|E3s], [E4|E4s], [E5|E5s], [E6|E6s], [E7|E7s], [E8|E8s]) :- call(Cont, E1, E2, E3, E4, E5, E6, E7), maplist(Cont, E1s, E2s, E3s, E4s, E5s, E6s, E7s, E8s). + + +sumlist_([], S, S). +sumlist_([N|Ns], S, S0) :- + S1 is S0 + N, + sumlist(Ns, S, S1). + +sumlist(Ns, S) :- + must_be(list, Ns), + sumlist_(Ns, S, 0).