From 9d5c9d3fe5d8951fac0902abf125c68a6e720e52 Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Tue, 13 Jul 2021 18:36:04 +0000 Subject: Implement sort/2 and setof/3 --- stdlib.pl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/stdlib.pl b/stdlib.pl index 7e4822d..803c0ee 100644 --- a/stdlib.pl +++ b/stdlib.pl @@ -299,6 +299,10 @@ bagof_next_s([H|T], WT_list, Rest) :- ; Rest = Rest0 ). +setof(Template, Goal, Instances) :- + bagof(Template, Goal, Instances_list), + sort(Instances_list, Instances). + % misc helpers variable_set(Term, []) :- @@ -357,3 +361,19 @@ variant_list([], []). variant_list([H1|T1], [H2|T2]) :- variant(H1, H2), variant_list(T1, T2). + +% Sorting, which also removes duplicates (should be implemented in C for speed I think). + +sort(Ls0, Ls) :- + append(Lefts, [A,B|Rights], Ls0), + A @> B, + !, + append(Lefts, [B,A|Rights], Ls1), + sort(Ls1, Ls). +sort(Ls0, Ls) :- + append(Lefts, [A,B|Rights], Ls0), + A == B, + !, + append(Lefts, [A|Rights], Ls1), + sort(Ls1, Ls). +sort(Ls, Ls). \ No newline at end of file -- cgit v1.2.3