Re: setof question

Gergö Barany <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On Sat, Mar 15, 2014 at 10:54:27 +0000, Roelof Wobben wrote:
> Im now experimenting with setof.

You probably shouldn't. You still seem to lack basic understanding of things
that you should understand before moving on to things like setof/3, which
behave in weird non-logical ways unlike most of the rest of the language.

That said...
 
> setof(X, parents(X,Y), X) 
>  
> Can I somehow print out both. 
> So the output will be something like this:
>  
> x = father  y=mother. 

This variant will try all bindings for X *separately* for each binding of Y:

?- setof(X, parents(X, Y), Parents).
Y = bob,
Parents = [alice] ;
Y = eve,
Parents = [adam] ;
Y = sally,
Parents = [harry].

You can change this behavior by saying that the X bindings for all bindings
of Y should be collected:

?- setof(X, Y^parents(X, Y), Parents).
Parents = [adam, alice, harry].

You can make pairs by using any two-place functor such as the popular -/2:

?- setof(X-Y, parents(X, Y), Parents).
Parents = [adam-eve, alice-bob, harry-sally].

Or construct any term of your liking:

?- setof(my_term(one_parent=X, other_parent=Y), parents(X, Y), Parents).
Parents = [my_term(one_parent=adam, other_parent=eve),
           my_term(one_parent=alice, other_parent=bob),
           my_term(one_parent=harry, other_parent=sally)].

(You are still using a term parents/2 without any indication which argument
is the father and which is the mother. Don't.)

-- 
Gergö Barany, research assistant                 [email protected]
Institute of Computer Languages      http://www.complang.tuwien.ac.at/gergo/
Vienna University of Technology                       Tel: +43-1-58801-58522
Argentinierstrasse 8/E185, 1040 Wien, Austria         Fax: +43-1-58801-18598
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.