0

I'm making a list of all the breed of dog sizes in my database, e.g.

breed(beagle,medium, hunting).
breed(bassets,medium, hunting).
breed(labrador,large, guideDogs).
breed(germanShepards,large, guardDogs).
breed(boxer,unknown,unknown).
breed(dalmation,unknown,unknown).
breed(ridgeback,unknown,unknown).

so I get the sizes from the database, but want to just have the unique sizes so that I can count them later. If I use

sizes(List) :- findall(Size, breed(_,Size,_), List).

I get duplicates e.g. [medium, medium, large, unknown, unknown, unknown]

setof/3 is meant to make the set unique, so i tried

sizes(List) :- setof(Size, breed(_,Size,_), List).

but it now only return the first entry [medium]

Any ideas why??

false
  • 10,264
  • 13
  • 101
  • 209
ildsarria
  • 281
  • 3
  • 10
  • 1
    Both queries are the same. – Willem Van Onsem Jul 10 '17 at 14:18
  • thanks, i have edited it – ildsarria Jul 10 '17 at 14:28
  • Give a minimal set of facts reproducing the issue. – Eugene Sh. Jul 10 '17 at 14:32
  • OK have just added the database that can reproduce the problem – ildsarria Jul 10 '17 at 14:38
  • 4
    The problem is the variables in your test condition that you don't care about when using `setof/3`. You need to use the existential quantifier: `setof(Size, Name^Type^breed(Name, Size, Type), List)`. Using `_` doesn't solve that for you because `_` will still be bound. The existential quantifier indicates that you don't wish to bind the variable. See the documentation, for example, [`bagof/3, setof/3`](http://www.gprolog.org/manual/html_node/gprolog033.html#sec114). – lurker Jul 10 '17 at 14:51
  • Do you mean like this?: sizes(List) :- setof(Size, breed(Type,Size,Job), List). I still got the same result. – ildsarria Jul 10 '17 at 14:57
  • 4
    No. I mean just like I wrote in my previous comment. :) `sizes(List) :- setof(Size, Type^Job^breed(Type,Size,Job), List).` Just using variables without the quantifier gives you the same problem: the variables will be bound and give you multiple, narrower solutions form `setof/3`. See the documentation. – lurker Jul 10 '17 at 14:58
  • Doh, awesome thank you – ildsarria Jul 10 '17 at 15:10

0 Answers0