Re: [eclipse-users] Question about setof/3 (and about _at_/1)

From: Joachim Schimpf <jschimpf_at_...5...>
Date: Fri, 27 Oct 2006 01:29:18 +0100
Ulrich Scholz wrote:
> Hi everybody,
> a question about setof/3: The manual states that the result is sorted but I
> could not find according to which ordering.  I guess, it's the standart term
> order.

Correct, the standard term order, as described in

> How could an implementation of setof/3 look like that uses an arbitrary,
> i.e., user-supplied ordering?

Use bagof/3 (or the faster findall/3), and sort the resulting list afterwards.

Alternatively, you could take advantage of the fact that the standard order
sorts on the leftmost argument of compound terms first.  So if you construct
your result terms such that the leftmost argument is your sorting key, then
the result list will automatically be ordered correctly:

% sorted alphabetically
?- setof(X, member(X,[bb,c,aaa]), L).
X = X
L = [aaa, bb, c]
Yes (0.00s cpu)

% sorted by length first
?- setof(N-X, (member(X,[bb,c,aaa]),atom_length(X,N)), L).
N = N
X = X
L = [1 - c, 2 - bb, 3 - aaa]
Yes (0.00s cpu)

> Thank you,
> Ulrich
> BTW, the manual states "This predicate is sensitive to its module context
> (tool predicate, see _at_/1)".  I guess, that sould read "... see @/2)". 

Quite right, thanks for spotting this!

-- Joachim
Received on Fri Oct 27 2006 - 01:29:54 CEST

This archive was generated by hypermail 2.3.0 : Mon Aug 26 2019 - 09:14:35 CEST