From 52488b875a35f697ffd3ca04419e57928f044f38 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sun, 1 Dec 2019 19:30:40 -0700 Subject: [PATCH] add predicates to lists.pl --- README.md | 3 +++ src/prolog/lib/lists.pl | 33 +++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 63614499..2d060807 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,7 @@ The following predicates are built-in to Scryer. * `false/0` * `findall/{3,4}` * `float/1` +* `foldl/{4,5}` * `forall/2` * `freeze/2` * `functor/3` @@ -219,6 +220,7 @@ The following predicates are built-in to Scryer. * `repeat/{0,1}` * `retract/1` * `reverse/2` +* `same_length/2` * `select/3` * `setof/3` * `setup_call_cleanup/3` @@ -226,6 +228,7 @@ The following predicates are built-in to Scryer. * `string/1` * `sub_atom/5` * `subsumes_term/2` +* `sumlist/2` * `term_expansion/2` * `term_variables/2` * `throw/1` diff --git a/src/prolog/lib/lists.pl b/src/prolog/lib/lists.pl index f3a4ef6b..153b90ff 100644 --- a/src/prolog/lib/lists.pl +++ b/src/prolog/lib/lists.pl @@ -1,7 +1,8 @@ -:- 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, sumlist/2]). +:- module(lists, [member/2, select/3, append/3, foldl/4, foldl/5, + memberchk/2, reverse/2, length/2, maplist/2, + maplist/3, maplist/4, maplist/5, maplist/6, + maplist/7, maplist/8, maplist/9, same_length/2, + sumlist/2]). :- use_module(library(error)). @@ -109,3 +110,27 @@ sumlist_([N|Ns], S, S0) :- sumlist(Ns, S) :- must_be(list, Ns), sumlist_(Ns, S, 0). + + + +same_length([], []). +same_length([_|As], [_|Bs]) :- + same_length(As, Bs). + + +foldl(Goal_3, Ls, A0, A) :- + foldl_(Ls, Goal_3, A0, A). + +foldl_([], _, A, A). +foldl_([L|Ls], G_3, A0, A) :- + call(G_3, L, A0, A1), + foldl_(Ls, G_3, A1, A). + + +foldl(Goal_4, Xs, Ys, A0, A) :- + foldl_(Xs, Ys, Goal_4, A0, A). + +foldl_([], [], _, A, A). +foldl_([X|Xs], [Y|Ys], G_4, A0, A) :- + call(G_4, X, Y, A0, A1), + foldl_(Xs, Ys, G_4, A1, A). -- 2.54.0