1

How do I get only one output from a SWI-Prolog query? I have tried using cut (!) but it does not seem to work.

For example: I already filled my knowledge base up with statements and I wanted to find any one name who is both female and is the mother of someone.

I have already tried:

mother(X,Y), female(X).

...but that gives me all of the X-__ and Y-__

I have also tried:

mother(X,Y), female(X), !.

... but that still gives me both the X-__ and Y__

I only want to find the X. Does anyone have any tips for me to somehow only get one?

lurker
  • 56,987
  • 9
  • 69
  • 103
user3369494
  • 123
  • 11
  • 2
    You're just guessing trying to use `!` (cut). :) It will just eliminate additional solutions for both `X` and `Y` resulting from backtracking. Did you try, `mother(X, _), female(X).`? The `_` means you don't care what it's value is. – lurker Feb 25 '15 at 02:17

1 Answers1

2
?- setof(t, Y^ ( mother(X, Y), female(Y) ), _).

which will remove duplicates (redundant answers/solutions), too. Or using library(lambda):

?- X+\ ( mother(X, Y), female(Y) ).

which does not remove redundant answers.

false
  • 10,264
  • 13
  • 101
  • 209
  • hmm, when I tried to use the "?- X+\ ( mother(X, Y), female(Y) ).", I got a ERROR: Syntax error: Operator expected ERROR: ERROR: ** here ** ERROR: X+\(mother(X,Y),female(Y)) what does that mean? – user3369494 Feb 25 '15 at 01:57
  • 1
    @user3369494: You need to use `library(lambda)`, see above. – false Feb 25 '15 at 01:59
  • ooh, my bad. I didn't see that. Sorry! Could you explain how the first line works? – user3369494 Feb 25 '15 at 02:07