Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!news.mathworks.com!newsfeed.internetmci.com!news.sprintlink.net!howland.reston.ans.net!ix.netcom.com!netcom.com!ludemann
From: ludemann@netcom.com (Peter Ludemann)
Subject: Re: problems with bagof
Message-ID: <ludemannDBzBL0.78n@netcom.com>
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
References: <3tbv33$7ps@due.unit.no> <ludemannDBIvv5.KCG@netcom.com> <3uclb9$d01@goanna.cs.rmit.edu.au> <3uj995$ph@lyra.csx.cam.ac.uk>
Date: Wed, 19 Jul 1995 20:00:35 GMT
Lines: 40
Sender: ludemann@netcom8.netcom.com

In article <3uj995$ph@lyra.csx.cam.ac.uk>,
Edmund Grimley-Evans <etg10@cl.cam.ac.uk> wrote:
>By the way, I found myself defining
>
>setof1(A,B,C) :-
>  findall(A,B,C0),
>  sort(C0,C).
>
>because I found it easier to have a goal that always succeeds
>exactly once.
>
>I suppose my setof1/3 is the same as the built-in setof/3 if one
>follows rule 1 ("avoid using existential quantifiers with
>bagof/setof").

No, because bagof/setof fails if there's no solution (Richard O'Keefe
has explained why; no need for me to repeat that).  I usually program
this by:

	( setof(X, p(X), L) -> true ; L = [] )

One other thing: I do hope you use more meaningful variable names in
your programs.  Something like this is better (costs nothing in
execution time, uses a tiny bit more space in your source file, aids
those who come after you):

setof1(Template, Pred, SetList) :-
	(  setof(Template, Pred, SetList)
	-> true
	;  SetList = []
	).

excercise for the reader: what is the difference (if any) between
	findall(Template, Pred, List)
and
	bagof(Template, Pred^Pred, List)
?

-- 
Peter Ludemann                      ludemann@netcom.com
