shithub: pprolog

Download patch

ref: 9d5c9d3fe5d8951fac0902abf125c68a6e720e52
parent: 9b4f17521bb8a7d96c09d540880679298ce6f08e
author: Peter Mikkelsen <[email protected]>
date: Tue Jul 13 14:36:04 EDT 2021

Implement sort/2 and setof/3

--- a/stdlib.pl
+++ b/stdlib.pl
@@ -299,6 +299,10 @@
 	; 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([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