diff options
author | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-07-13 18:36:04 +0000 |
---|---|---|
committer | Peter Mikkelsen <peter@pmikkelsen.com> | 2021-07-13 18:36:04 +0000 |
commit | 9d5c9d3fe5d8951fac0902abf125c68a6e720e52 (patch) | |
tree | 3f46f3efabc7c3edf0815fcc411df93f6d6f5814 | |
parent | 9b4f17521bb8a7d96c09d540880679298ce6f08e (diff) |
Implement sort/2 and setof/3
-rw-r--r-- | stdlib.pl | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -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 |